- Reference >
- JavaScript Methods >
- db.collection.find()
db.collection.find()¶
-
db.collection.find(query, projection)¶ The
find()method selects documents in a collection and returns a cursor to the selected documents.The
find()method takes the following parameters.Parameters: - query (document) – Optional. Specifies the selection criteria
using query operators. Omit the
queryparameter or pass an empty document (e.g.{}) to return all documents in the collection. - projection (document) –
Optional. Controls the fields to return, or the projection. The
projectionargument will resemble the following prototype:The
booleancan take the following include or exclude values:1ortrueto include. Thefind()method always includes the _id field even if the field is not explicitly stated to return in the projection parameter.0orfalseto exclude.
The
projectioncannot contain both include and exclude specifications except for the exclusion of the_idfield.Omit the
projectionparameter to return all the fields in the matching documents.
Returns: A cursor to the documents that match the
querycriteria. If theprojectionargument is specified, the matching documents contain only theprojectionfields, and the_idfield if you do not explicitly exclude the_idfield.Note
In the
mongoshell, you can access the returned documents directly without explicitly using the JavaScript cursor handling method. Executing the query directly on themongoshell prompt automatically iterates the cursor to display up to the first 20 documents. Typeitto continue iteration.Consider the following examples of the
find()method:To select all documents in a collection, call the
find()method with no parameters:This operation returns all the documents with all the fields from the collection
products. By default, in themongoshell, the cursor returns the first batch of 20 matching documents. In themongoshell, iterate through the next batch by typingit. Use the appropriate cursor handling mechanism for your specific language driver.To select the documents that match a selection criteria, call the
find()method with thequerycriteria:This operation returns all the documents from the collection
productswhereqtyis greater than25, including all fields.To select the documents that match a selection criteria and return, or project only certain fields into the result set, call the
find()method with thequerycriteria and theprojectionparameter, as in the following example:This operation returns all the documents from the collection
productswhereqtyis greater than25. The documents in the result set only include the_id,item, andqtyfields using “inclusion” projection.find()always returns the_idfield, even when not explicitly included:To select the documents that match a query criteria and exclude a set of fields from the resulting documents, call the
find()method with thequerycriteria and theprojectionparameter using theexcludesyntax:The query will return all the documents from the collection
productswhereqtyis greater than25. The documents in the result set will contain all fields except the_idandqtyfields, as in the following:
- query (document) – Optional. Specifies the selection criteria
using query operators. Omit the