w3resource
Mongodb Tutorial

MongoDB: db.collection.find() method

db.collection.find

The db.collection.find() method is used to perform a query on a document in a collection and returns a cursor to the selected documents.

Syntax:

db.collection.find(query, projection)

Parameters:

Name Description Required /
Optional
Type
query Specifies selection criteria using query operators. To return all documents in a collection, omit this parameter or pass an empty document ({}). Optional document
projection Specifies the fields to return using projection operators. To return all fields in the matching document, omit this parameter. Optional document

The projection parameter takes a document of the following form:


{ field1: <boolean>, field2: <boolean> ... }

The <boolean> value can be any of the following:

  • 1 or true to include the field. The find() method always includes the _id field even if the field is not explicitly stated to return in the projection parameter.
  • 0 or false to exclude the field.

A projection cannot contain both include and exclude specifications, except for the exclusion of the _id field. In projections that explicitly include fields, the _id field is the only field that you can explicitly exclude.

Returns:

  • A cursor to the documents that match the query criteria. When the find() method “returns documents,” the method is actually returning a cursor to the documents.
  • If find() includes a projection argument, the matching documents contain only the projection fields and the _id field. You can optionally exclude the _id field.
  • Executing find() directly in the mongo shell automatically iterates the cursor to display up to the first 20 documents. Type it to continue iteration.
  • To access the returned documents with a driver, use the appropriate cursor handling mechanism for the driver language.

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: Find all documents in a collection

The following example will retrieve all the documents from the collection restaurants.

> db.restaurants.find();

Output:

> db.restaurants.find();
{ "_id" : ObjectId("55c3043ab165fa6355ec5c89"), "address" : { "building" : "1007", "coord" : [ -73.856077, 40.848447 ], "street" : "Morris Park Ave", "zipcode" : "10462" }, "borough" : "Bronx", "cuisine" : "Bakery", "grades" : [ { "date" : ISODate("2014-03-03T00:00:00Z"), "grade" : "A", "score" : 2
}, { "date" : ISODate("2013-09-11T00:00:00Z"), "grade" : "A", "score" : 6 }, { "date" : ISODate("2013-01-24T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-11-23T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2011-03-10T00:00:00Z"), "grade" : "B", "score" : 14 }
], "name" : "Morris Park Bake Shop", "restaurant_id" : "30075445" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5c8a"), "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate("2014-12-30T00:00:00Z"), "grade" : "A", "score
" : 8 }, { "date" : ISODate("2014-07-01T00:00:00Z"), "grade" : "B", "score" : 23 }, { "date" : ISODate("2013-04-30T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-05-08T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5c8b"), "address" : { "building" : "351", "coord" : [ -73.98513559999999, 40.7676919 ], "street" : "West   57 Street", "zipcode" : "10019" }, "borough" : "Manhattan", "cuisine" : "Irish", "grades" : [ { "date" : ISODate("2014-09-06T00:00:00Z"), "grade" : "A",
"score" : 2 }, { "date" : ISODate("2013-07-22T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-07-31T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2011-12-29T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Dj Reynolds Pub And Restaurant", "restaurant_id"
 : "30191841" }
{ "_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"
 }
{ "_id" : ObjectId("55c3043ab165fa6355ec5c8d"), "address" : { "building" : "97-22", "coord" : [ -73.8601152, 40.7311739 ], "street" : "63 Road", "zipcode" : "11374" }, "borough" : "Queens", "cuisine" : "Jewish/Kosher", "grades" : [ { "date" : ISODate("2014-11-24T00:00:00Z"), "grade" : "Z", "score" :
 20 }, { "date" : ISODate("2013-01-17T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-08-02T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2011-12-15T00:00:00Z"), "grade" : "B", "score" : 25 } ], "name" : "Tov Kosher Kitchen", "restaurant_id" : "40356068" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5c8e"), "address" : { "building" : "8825", "coord" : [ -73.8803827, 40.7643124 ], "street" : "Astoria Boulevard", "zipcode" : "11369" }, "borough" : "Queens", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-11-15T00:00:00Z"), "grade" : "Z", "sco
re" : 38 }, { "date" : ISODate("2014-05-02T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2013-03-02T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-02-10T00:00:00Z"), "grade" : "A", "score" : 13 } ], "name" : "Brunos On The Boulevard", "restaurant_id" : "4035615
1" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5c8f"), "address" : { "building" : "2206", "coord" : [ -74.1377286, 40.6119572 ], "street" : "Victory Boulevard", "zipcode" : "10314" }, "borough" : "Staten Island", "cuisine" : "Jewish/Kosher", "grades" : [ { "date" : ISODate("2014-10-06T00:00:00Z"), "grade"
: "A", "score" : 9 }, { "date" : ISODate("2014-05-20T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-04-04T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISO Date("2012-01-24T00:00:00Z"), "grade" : "A", "score" : 9 } ], "name" : "Kosher Island", "restaurant _id" : "4035644
2" }
.........

Example: Find documents that Match Query Criteria

The following example will retrieve all the documents from the collection restaurants which matches the query criteria cord is more than 110.

db.restaurants.find( { "address.cord": { $get: 110 } } );

Output:

> db.restaurants.find( { "address.cord": { $get: 110 } } );
{ "_id" : ObjectId("55c3043ab165fa6355ec5d73"), "address" : { "building" : "155157", "coord" : [ 153.1628795, -28.0168595 ], "street" : "Christie St.", "zipcode" : "10002" }, "borough" : "Manhattan", "cuisine" : "Steak", "grades" : [ { "date" : ISODate("2014-11-12T00:00:00Z"), "grade" : "A", "score"
 : 12 }, { "date" : ISODate("2013-09-24T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2013-04-12T00:00:00Z"), "grade" : "B", "score" : 26 }, { "date" : ISODate("2012-09-21T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2012-04-10T00:00:00Z"), "grade" : "A", "score" :
 2 } ], "name" : "Sammy'S Steakhouse", "restaurant_id" : "40368552" }

Example: Find documents using Query for Equality

The following example will retrieve all the documents from the collection restaurants which satisfy the equality test cuisine is Ethiopian.

db.restaurants.find( {"cuisine" : "Ethiopian" } );

Output:

> db.restaurants.find( {"cuisine" : "Ethiopian" } );
{ "_id" : ObjectId("55c3043ab165fa6355ec5f2d"), "address" : { "building" : "1239", "coord" : [ -73.9585846, 40.8097806 ], "street" : "Amsterdam Avenue", "zipcode" : "10027" }, "borough" : "Manhattan", "cuisine" : "Ethiopian", "grades" : [ { "date" : ISODate("2014-03-03T00:00:00Z"), "grade" : "A", "s
core" : 9 }, { "date" : ISODate("2013-02-02T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2011-12-27T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2011-09-23T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Masawa", "restaurant_id" : "40388091" }
{ "_id" : ObjectId("55c3043ab165fa6355ec68d6"), "address" : { "building" : "124", "coord" : [ -74.00024599999999, 40.729905 ], "street" : "Macdougal Street", "zipcode" : "10012" }, "borough" : "Manhattan", "cuisine" : "Ethiopian", "grades" : [ { "date" : ISODate("2014-08-08T00:00:00Z"), "grade" : "A
", "score" : 13 }, { "date" : ISODate("2013-07-18T00:00:00Z"), "grade" : "A", "score" : 4 }, { "date" : ISODate("2012-07-17T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2011-12-12T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Meskerem", "restaurant_id" : "40792697" }
{ "_id" : ObjectId("55c3043ab165fa6355ec6943"), "address" : { "building" : "650", "coord" : [ -73.99346299999999, 40.761925 ], "street" : "10 Avenue", "zipcode" : "10036" }, "borough" : "Manhattan", "cuisine" : "Ethiopian", "grades" : [ { "date" : ISODate("2014-07-14T00:00:00Z"), "grade" : "A", "sco
re" : 12 }, { "date" : ISODate("2014-01-14T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-02-12T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Queen Of Sheba Ethiopian Restaurant", "restaurant_id" : "40813634" }
{ "_id" : ObjectId("55c3043ab165fa6355ec7000"), "address" : { "building" : "338", "coord" : [ -73.9868924, 40.7264833 ], "street" : "East    6 Street", "zipcode" : "10003" }, "borough" : "Manhattan", "cuisine" : "Ethiopian", "grades" : [ { "date" : ISODate("2014-08-15T00:00:00Z"), "grade" : "A", "sc
ore" : 10 }, { "date" : ISODate("2013-07-12T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-07-16T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-02-27T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2011-09-20T00:00:00Z"), "grade" : "A", "sco
re" : 13 } ], "name" : "Awash Ethiopian Restaurant", "restaurant_id" : "41044592" }
{ "_id" : ObjectId("55c3043ab165fa6355ec7412"), "address" : { "building" : "2084", "coord" : [ -73.9565946, 40.8022824 ], "street" : "Fredrick Douglas Boulevard", "zipcode" : "10026" }, "borough" : "Manhattan", "cuisine" : "Ethiopian", "grades" : [ { "date" : ISODate("2014-08-28T00:00:00Z"), "grade"
 : "A", "score" : 8 }, { "date" : ISODate("2014-01-10T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2013-03-15T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-03-13T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-08-30T00:00:00Z"), "grade" :
"A", "score" : 6 } ], "name" : "Zoma", "restaurant_id" : "41156869" }
......

Example: Find documents using Operators

The following example will retrieve all the documents from the collection restaurants which satisfy the criteria, that is score is within the specified value.

db.restaurants.find({"grades.score" : {$in : [35,40,42,44,46]}});

Output:

> db.restaurants.find({"grades.score" : {$in : [35,40,42,44,46]}});
{ "_id" : ObjectId("55c3043ab165fa6355ec5ccd"), "address" : { "building" : "180", "coord" : [ -73.9788694, 40.7665961 ], "street" : "Central Park South", "zipcode" : "10019" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-12-15T00:00:00Z"), "grade" : "A", "
score" : 12 }, { "date" : ISODate("2014-08-07T00:00:00Z"), "grade" : "C", "score" : 40 }, { "date" : ISODate("2013-07-29T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2012-12-13T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-07-30T00:00:00Z"), "grade" : "C", "s
core" : 4 }, { "date" : ISODate("2012-02-16T00:00:00Z"), "grade" : "A", "score" : 2 } ], "name" : "Nyac Main Dining Room", "restaurant_id" : "40364467" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5cfe"), "address" : { "building" : "910", "coord" : [ -73.9799932, 40.7660886 ], "street" : "Seventh Avenue", "zipcode" : "10019" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2015-01-08T00:00:00Z"), "grade" : "Z", "scor
e" : 35 }, { "date" : ISODate("2014-06-02T00:00:00Z"), "grade" : "B", "score" : 19 }, { "date" : ISODate("2013-11-25T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-06-24T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2012-12-04T00:00:00Z"), "grade" : "B", "score
" : 24 }, { "date" : ISODate("2012-06-14T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-02-24T00:00:00Z"), "grade" : "B", "score" : 21 } ], "name" : "La Parisienne Diner", "restaurant_id" : "40365633" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5d22"), "address" : { "building" : "5820", "coord" : [ -73.9002615, 40.885186 ], "street" : "Broadway", "zipcode" : "10463" }, "borough" : "Bronx", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-02-26T00:00:00Z"), "grade" : "A", "score" : 5 },
{ "date" : ISODate("2013-10-09T00:00:00Z"), "grade" : "B", "score" : 19 }, { "date" : ISODate("2013-05-15T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2012-11-20T00:00:00Z"), "grade" : "B", "score" : 18 }, { "date" : ISODate("2011-10-17T00:00:00Z"), "grade" : "A", "score" : 10 }, {
 "date" : ISODate("2011-06-22T00:00:00Z"), "grade" : "C", "score" : 35 } ], "name" : "The Punch Bowl", "restaurant_id" : "40366497" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5d4d"), "address" : { "building" : "1372", "coord" : [ -73.9870818, 40.7523004 ], "street" : "Broadway", "zipcode" : "10018" }, "borough" : "Manhattan", "cuisine" : "Jewish/Kosher", "grades" : [ { "date" : ISODate("2014-05-01T00:00:00Z"), "grade" : "A", "score
" : 12 }, { "date" : ISODate("2013-11-04T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2013-05-14T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-10-15T00:00:00Z"), "grade" : "C", "score" : 40 }, { "date" : ISODate("2011-10-18T00:00:00Z"), "grade" : "A", "score
" : 12 }, { "date" : ISODate("2011-05-27T00:00:00Z"), "grade" : "A", "score" : 5 } ], "name" : "Mr Broadway Kosher Restaurant", "restaurant_id" : "40367534" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5da0"), "address" : { "building" : "1011", "coord" : [ -73.9623333, 40.7757194 ], "street" : "Madison Avenue", "zipcode" : "10075" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-05-02T00:00:00Z"), "grade" : "A", "sco
re" : 10 }, { "date" : ISODate("2013-10-21T00:00:00Z"), "grade" : "B", "score" : 15 }, { "date" : ISODate("2013-04-19T00:00:00Z"), "grade" : "B", "score" : 0 }, { "date" : ISODate("2012-11-21T00:00:00Z"), "grade" : "C", "score" : 40 }, { "date" : ISODate("2012-04-09T00:00:00Z"), "grade" : "B", "scor
e" : 17 } ], "name" : "Viand Cafe", "restaurant_id" : "40369753" }

Example: Find documents including and excluding specific field

The following example will retrieve all the documents from the collection restaurants including the field borough and cuisine and excluding the field _id which satisfy the criteria, that is coord is more than 100.

db.restaurants.find({ "address.coord": { $gt: 100 }},{ borough: 1, cuisine: 1, address: 1 ,_id :0});

Output:

> db.restaurants.find({ "address.coord": { $gt: 100 }},{ borough: 1, cuisine: 1, address: 1 ,_id :0});
{ "address" : { "building" : "155157", "coord" : [ 153.1628795, -28.0168595 ], "street" : "Christie St.", "zipcode" : "10002" }, "borough" : "Manhattan", "cuisine" : "Steak" }
{ "address" : { "building" : "14", "coord" : [ 104.9179596, 11.5415182 ], "street" : "West Wing", "zipcode" : "11430" }, "borough" : "Queens", "cuisine" : "Café/Coffee/Tea" }
{ "address" : { "building" : "14", "coord" : [ 104.9179596, 11.5415182 ], "street" : "West Wing", "zipcode" : "11430" }, "borough" : "Queens", "cuisine" : "Continental" }
{ "address" : { "building" : "NKA", "coord" : [ 100.6294417, 13.7440888 ], "street" : "@ West 15Th Street", "zipcode" : "10004" }, "borough" : "Manhattan", "cuisine" : "Mexican" }
{ "address" : { "building" : "W 30TH ST", "coord" : [ 100.6177619, 13.7085011 ], "street" : "10 Th Ave", "zipcode" : "10001" }, "borough" : "Manhattan", "cuisine" : "American " }

Retrieve the restaurants data from here

Previous: db.collection.explain() method
Next: db.collection. findAndModify() method



New Content: Composer: Dependency manager for PHP, R Programming