db.collection.find()

Definition

db.collection.find(query, projection)

Selects documents in a collection and returns a cursor to the selected documents.

Parameter Type Description
query document Optional. Specifies selection criteria using query operators. To return all documents in a collection, omit this parameter or pass an empty document ({}).
projection document Optional. Specifies the fields to return using projection operators. To return all fields in the matching document, omit this parameter.
Returns:A cursor to the documents that match the query criteria. If the projection argument is specified, the matching documents contain only the projection fields, and the _id field if you do not explicitly exclude the _id field.

In the mongo shell, you can access the returned documents directly without explicitly using the JavaScript cursor handling method. Executing the query directly on the mongo shell prompt automatically iterates the cursor to display up to the first 20 documents. Type it to continue iteration.

Examples

Select All Documents in a Collection

Call the find() method with no parameters:

db.products.find()

This returns all the documents with all the fields from the collection products. In the mongo shell, the cursor returns the first batch of 20 matching documents by default. Iterate through the next batch by typing it. Use the appropriate cursor handling mechanism for your specific language driver.

Select Documents that Match a Selection Criteria

Call the find() method with the query criteria:

db.products.find( { qty: { $gt: 25 } } )

This returns all the documents from the collection products where qty is greater than 25, including all fields.

Prototype Projection Document

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

The boolean can take the following include or exclude values:

  • 1 or true to include. 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 projection cannot contain both include and exclude specifications except for the exclusion of the _id field.

Project Certain Fields in the Result Set

The following example selects documents that match a selection criteria and returns, or projects, only certain fields into the result set:

db.products.find( { qty: { $gt: 25 } }, { item: 1, qty: 1 } )

This returns all the documents from the collection products where qty is greater than 25. The documents in the result set only include the _id, item, and qty fields using “inclusion” projection. find() always returns the _id field, even when not explicitly included:

{ "_id" : 11, "item" : "pencil", "qty" : 50 }
{ "_id" : ObjectId("50634d86be4617f17bb159cd"), "item" : "bottle", "qty" : 30 }
{ "_id" : ObjectId("50634dbcbe4617f17bb159d0"), "item" : "paper", "qty" : 100 }

Exclude Certain Fields from the Result Set

The following example selects documents that match a selection criteria and excludes a set of fields from the resulting documents:

db.products.find( { qty: { $gt: 25 } }, { _id: 0, qty: 0 } )

The query returns all the documents from the collection products where qty is greater than 25. The documents in the result set will contain all fields except the _id and qty fields, as in the following:

{ "item" : "pencil", "type" : "no.2" }
{ "item" : "bottle", "type" : "blue" }
{ "item" : "paper" }

MongoDB Manual 2.4

Formats

About MongoDB

MongoDB Ecosystem

MongoDB Resources