Saturday 5 June 2021

Mongo shell: why pretty method fails on findOne method

‘pretty()’ method runs on a cursor. Since findOne() method do not return a cursor, pretty() method will not work on it.

> db.audit.findOne()
{
      "_id" : ObjectId("60bb0156baf44d88348459c4"),
      "type" : "login",
      "modifiedTimestamp" : 1622908612185
}
> 
> db.audit.findOne().pretty()
uncaught exception: TypeError: db.audit.findOne(...).pretty is not a function :
@(shell):1:1

Since find() method return a cursor, you can use pretty() method with find() method.

> db.audit.find().pretty()
{
      "_id" : ObjectId("60bb0156baf44d88348459c4"),
      "type" : "login",
      "modifiedTimestamp" : 1622908612185
}
{
      "_id" : ObjectId("60bb9c348251689f64fb3ece"),
      "type" : "logout",
      "client" : "browser",
      "userId" : 234,
      "createdTimestamp" : 1622907956987
}
{
      "_id" : ObjectId("60bb9c418251689f64fb3ecf"),
      "type" : "login",
      "modifiedTimestamp" : 1622908670676
}
{
      "_id" : ObjectId("60bb9c598251689f64fb3ed0"),
      "type" : "reset_credentials",
      "client" : "mobile",
      "userId" : 31,
      "createdTimestamp" : 1622907993918
}
{
      "_id" : ObjectId("60bb9c6f8251689f64fb3ed1"),
      "type" : "login",
      "client" : "mobile",
      "userId" : 41,
      "createdTimestamp" : 1622908015960,
      "modifiedTimestamp" : 1622908909572
}

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment