- MongoDB CRUD Operations >
- Query Documents
Query Documents¶
This page provides examples in:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
This page provides examples of query operations using the
db.collection.find()
method in the
mongo
shell. The examples on this page use the
inventory
collection. To populate the inventory
collection, run the following:
This page provides examples of query operations using
MongoDB Compass. The examples on this
page use the inventory
collection. Populate the
inventory
collection with the following documents:
This page provides examples of query operations using the
pymongo.collection.Collection.find()
method in the
PyMongo
Python driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the
following:
This page provides examples of query operations using the com.mongodb.client.MongoCollection.find method in the MongoDB Java Synchronous Driver.
Tip
The driver provides com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. The examples on this page use these methods to create the filter documents.
The examples on this page use the inventory
collection. To populate the inventory
collection, run the
following:
This page provides examples of query operations using the
Collection.find() method in
the MongoDB Node.js Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
This page provides examples of query operations using the
MongoDB\Collection::find()
method in the
MongoDB PHP Library.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
This page provides examples of query operations using the
motor.motor_asyncio.AsyncIOMotorCollection.find()
method in the Motor
driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the
following:
This page provides examples of query operations using the com.mongodb.reactivestreams.client.MongoCollection.find method in the MongoDB Java Reactive Streams Driver.
The examples on this page use the inventory
collection. To populate the inventory
collection, run the
following:
This page provides examples of query operations using the
MongoCollection.Find()
method in the
MongoDB C# Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
This page provides examples of query operations using the
MongoDB::Collection::find() method
in the
MongoDB Perl Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
This page provides examples of query operations using the
Mongo::Collection#find()
method in the
MongoDB Ruby Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
This page provides examples of query operations using the
collection.find() method
in the
MongoDB Scala Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
This page provides examples of query operations using the
Collection.Find
function in the
MongoDB Go Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
You can run the operation in the web shell below:
For instructions on inserting documents in MongoDB Compass, see Insert Documents.
Select All Documents in a Collection¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
To select all documents in the collection, pass an empty document as the query filter parameter to the query bar. The query filter parameter determines the select criteria:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
This operation corresponds to the following SQL statement:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
For more information on the syntax of the method, see
find()
.
For more information on the MongoDB Compass query bar, see Query Bar.
For more information on the syntax of the method, see
find()
.
For more information on the syntax of the method, see com.mongodb.client.MongoCollection.find.
For more information on the syntax of the method, see find().
For more information on the syntax of the method, see
find()
.
For more information on the syntax of the method, see com.mongodb.reactivestreams.client.MongoCollection.find.
For more information on the syntax of the method, see Find().
For more information on the syntax of the method, see find().
For more information on the syntax of the method, see find().
For more information on the syntax of the method, see collection.find().
Specify Equality Condition¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
To specify equality conditions, use <field>:<value>
expressions in the
query filter document:
To specify equality conditions, use <field>:<value>
expressions in the
query filter document:
To specify equality conditions, use <field>:<value>
expressions in the
query filter document:
To specify equality conditions, use the
com.mongodb.client.model.Filters.eq_
method to create the
query filter document:
To specify equality conditions, use <field>:<value>
expressions in the
query filter document:
To specify equality conditions, use <field> => <value>
expressions in the
query filter document:
To specify equality conditions, use <field>:<value>
expressions in the
query filter document:
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:
To specify equality conditions, construct a filter using the Eq method:
To specify equality conditions, use <field> => <value>
expressions in the
query filter document:
To specify equality conditions, use <field> => <value>
expressions in the
query filter document:
To specify equality conditions, use the
com.mongodb.client.model.Filters.eq_
method to create the
query filter document:
The following example selects from the inventory
collection all
documents where the status
equals "D"
:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following filter into the Compass query bar and click Find:
This operation corresponds to the following SQL statement:
- Compass
Note
The MongoDB Compass query bar autocompletes the current query based on the keys in your collection’s documents, including keys in embedded sub-documents.
Specify Conditions Using Query Operators¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
A query filter document can use the query operators to specify conditions in the following form:
A query filter document can use the query operators to specify conditions in the following form:
A query filter document can use the query operators to specify conditions in the following form:
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
A query filter document can use the query operators to specify conditions in the following form:
A query filter document can use the query operators to specify conditions in the following form:
A query filter document can use the query operators to specify conditions in the following form:
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:
A query filter document can use the query operators to specify conditions in the following form:
A query filter document can use the query operators to specify conditions in the following form:
In addition to the equality condition, MongoDB provides
various query operators to specify
filter conditions. Use the
com.mongodb.client.model.Filters_
helper methods to
facilitate the creation of filter documents. For example:
The following example retrieves all documents from the inventory
collection where status
equals either "A"
or "D"
:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following filter into the Compass query bar and click Find:
Note
Although you can express this query using the $or
operator,
use the $in
operator rather than the $or
operator when performing equality checks on the same field.
The operation corresponds to the following SQL statement:
Refer to the Query and Projection Operators document for the complete list of MongoDB query operators.
Specify AND
Conditions¶
A compound query can specify conditions for more than one field in the
collection’s documents. Implicitly, a logical AND
conjunction
connects the clauses of a compound query so that the query selects the
documents in the collection that match all the conditions.
The following example retrieves all documents in the inventory
collection where the status
equals "A"
and qty
is less
than ($lt
) 30
:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following filter into the Compass query bar and click Find:
The operation corresponds to the following SQL statement:
See comparison operators for other MongoDB comparison operators.
Specify OR
Conditions¶
Using the $or
operator, you can specify a compound query
that joins each clause with a logical OR
conjunction so that the
query selects the documents in the collection that match at least one
condition.
The following example retrieves all documents in the collection where
the status
equals "A"
or qty
is less than
($lt
) 30
:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following filter into the Compass query bar and click Find:
The operation corresponds to the following SQL statement:
Note
Queries which use comparison operators are subject to Type Bracketing.
Specify AND
as well as OR
Conditions¶
In the following example, the compound query document selects all
documents in the collection where the status
equals "A"
and either qty
is less than ($lt
) 30
or
item
starts with the character p
:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following filter into the Compass query bar and click Find:
The operation corresponds to the following SQL statement:
Note
MongoDB supports regular expressions $regex
queries to
perform string pattern matches.
Additional Query Tutorials¶
For additional query examples, see:
Behavior¶
Cursor¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
The db.collection.find()
method
returns a cursor to the matching
documents.
The MongoDB Compass Find operation opens a cursor to the matching documents of the collection based on the find query.
For more information on sampling in MongoDB Compass, see the Compass FAQ.
The pymongo.collection.Collection.find()
method
returns a cursor to the
matching documents. See the PyMongo documentation for
iterating over a cursor.
The com.mongodb.client.MongoCollection.find method returns an instance of the com.mongodb.client.FindIterable interface.
The Collection.find() method returns a cursor.
The MongoDB\Collection::find()
method returns a cursor to
the matching documents. See the MongoDB PHP Library
documentation for
iterating over a cursor.
com.mongodb.reactivestreams.client.MongoCollection.find returns an instance of the com.mongodb.reactivestreams.client.FindPublisher interface.
The MongoCollection.Find() method returns a cursor to the matching documents. See the MongoDB C# driver documentation for iterating over a cursor.
The MongoDB::Collection::find() method returns a cursor to the matching documents. See the MongoDB Perl driver documentation for iterating over a cursor.
The Mongo::Collection#find()
method returns a CollectionView,
which is an Enumerable
. A Cursor is
created when the View
is enumerated; for example, by calling
#to_a()
or #each()
. You can also get an Enumerator
by calling
#to_enum()
on the View
. See the Ruby driver API documentation
for iterating over a cursor.
The collection.find() method returns the find Observable.
The Collection.Find function returns a Cursor to the matching documents. See the Cursor documentation for more information.
Read Isolation¶
New in version 3.2.
For reads to replica sets and replica set shards, read concern allows clients to choose a level of isolation for their reads. For more information, see Read Concern.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Additional Methods
The following methods can also read documents from a collection:
db.collection.findOne
- In aggregation pipeline, the
$match
pipeline stage provides access to MongoDB queries.
Note
The db.collection.findOne()
method also performs a read
operation to return a single document. Internally, the
db.collection.findOne()
method is the
db.collection.find()
method with a limit of 1.
Additional Options
In addition to filter
, MongoDB Compass also allows the
following options to be passed to the query bar:
Project | Specify which fields to return in the resulting data. |
Sort | Specify the sort order of the returned documents. |
Skip | Specify the first n-number of document to skip before returning the result set. |
Limit | Specify the maximum number of documents to return. |
Additional Methods
The following methods can also read documents from a collection:
pymongo.collection.Collection.find_one()
- In aggregation pipeline, the
$match
pipeline stage provides access to MongoDB queries. See the PyMongo Aggregation Examples.
Note
The pymongo.collection.Collection.find_one()
method also performs a read operation to return a single
document. Internally, the
pymongo.collection.Collection.find_one()
method is
the pymongo.collection.Collection.find()
method
with a limit of 1.
Additional Methods
The following methods can also read documents from a collection:
- In the aggregation pipeline,
the
$match
pipeline stage provides access to MongoDB queries. See the Java Synchronous Driver Aggregation Examples.
Additional Methods
The following methods can also read documents from a collection:
- Collection.findOne()
- In aggregation pipeline, the
$match
pipeline stage provides access to MongoDB queries. See the MongoDB Node.js Driver’s aggregation tutorial.
Note
The Collection.findOne() method also performs a read operation to return a single document. Internally, the Collection.findOne() method is the Collection.find() method with a limit of 1.
Additional Methods
The following methods can also read documents from a collection:
MongoDB\Collection::findOne()
- In aggregation pipeline, the
$match
pipeline stage provides access to MongoDB queries. See the MongoDB PHP Library’s aggregation example.
Note
The MongoDB\Collection::findOne()
method also performs a read operation to return a single
document. Internally, the
MongoDB\Collection::findOne()
method is the
MongoDB\Collection::find()
method with a limit of 1.
Additional Methods
The following methods can also read documents from a collection:
- In aggregation pipeline,
the
$match
pipeline stage provides access to MongoDB queries. See com.mongodb.reactivestreams.client.MongoCollection.aggregate for more information.
Additional Methods
The following methods can also read documents from a collection:
- MongoCollection.FindOne()
- In aggregation pipeline, the
$match
pipeline stage provides access to MongoDB queries. See the MongoDB C# driver’s LINQ documentation.
Note
The MongoCollection.FindOne() method also performs a read operation to return a single document. Internally, the MongoCollection.FindOne() method is the MongoCollection.Find() method with a limit of 1.
Additional Methods
The following methods can also read documents from a collection:
- MongoDB::Collection::find_one()
- In aggregation pipeline, the
$match
pipeline stage provides access to MongoDB queries. See the MongoDB Perl driver’s aggregation examples.
Note
The MongoDB::Collection::find_one() method also performs a read operation to return a single document. Internally, the MongoDB::Collection::find_one() method is the MongoDB::Collection::find() method with a limit of 1.
Additional Methods
The following methods can also read documents from a collection:
- In aggregation pipeline, the
$match
pipeline stage provides access to MongoDB queries. See the MongoDB Ruby driver’s aggregation examples.
Additional Methods
The following methods can also read documents from a collection:
- In aggregation pipeline, the
$match
pipeline stage provides access to MongoDB queries. See the MongoDB Scala driver’s aggregate method.
Additional Methods
The following methods can also read documents from a collection:
- Collection.FindOne
- In aggregation pipeline,
the
$match
pipeline stage provides access to MongoDB queries. See Collection.Aggregate.