w3resource
MongoDB Tutorial

MongoDB: cursor.pretty() method

cursor.pretty

The cursor.pretty() method is used to configure the cursor to display results in an easy-to-read format.

Syntax:

cursor.pretty()

The pretty() method has the following prototype form:

db.collection.find(<query>).pretty()

Sample document in the restaurants collection:


{
  "address": {
     "building": "1007",
     "coord": [ -73.856077, 40.848447 ],
     "street": "Morris Park Ave",
     "zipcode": "10462"
  },
  "borough": "Bronx",
  "cuisine": "Bakery",
  "grades": [
     { "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
     { "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
     { "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
     { "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
     { "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
  ],
  "name": "Morris Park Bake Shop",
  "restaurant_id": "30075445"
}
.....

Example: MongoDB: cursor.pretty() method

The following example configure the output of the query from restaurants collection which match the field cuisine is equal to American with output limit is one.

db.restaurants.find({"cuisine" : "American "}).limit(1).pretty();

Output:

> db.restaurants.find({"cuisine" : "American "}).limit(1).pretty();
{
        "_id" : ObjectId("55c3043ab165fa6355ec5c8c"),
        "address" : {
                "building" : "2780",
                "coord" : [
                        -73.98241999999999,
                        40.579505
                ],
                "street" : "Stillwell Avenue",
                "zipcode" : "11224"
        },
        "borough" : "Brooklyn",
        "cuisine" : "American ",
        "grades" : [
                {
                        "date" : ISODate("2014-06-10T00:00:00Z"),
                        "grade" : "A",
                        "score" : 5
                },
                {
                        "date" : ISODate("2013-06-05T00:00:00Z"),
                        "grade" : "A",
                        "score" : 7
                },
                {
                        "date" : ISODate("2012-04-13T00:00:00Z"),
                        "grade" : "A",
                        "score" : 12
                },
                {
                        "date" : ISODate("2011-10-12T00:00:00Z"),
                        "grade" : "A",
                        "score" : 12
                }
        ],
        "name" : "Riviera Caterer",
        "restaurant_id" : "40356018"
}

Retrieve the restaurants data from here

Previous: cursor.objsLeftInBatch() method
Next: cursor.readPref() method