Docs Menu
Docs Home
/
MongoDB Manual
/

Query Documents

On this page

  • Select All Documents in a Collection
  • Specify Equality Condition
  • Specify Conditions Using Query Operators
  • Specify AND Conditions
  • Specify OR Conditions
  • Specify AND as well as OR Conditions
  • Query Documents with MongoDB Atlas
  • Additional Query Tutorials
  • Behavior
  • Additional Methods and Options

You can query documents in MongoDB by using the following methods:

  • Your programming language's driver.

  • The MongoDB Atlas UI. To learn more, see Query Documents with MongoDB Atlas.

  • MongoDB Compass.


Use the Select your language drop-down menu in the upper-right to set the language of the following examples or select MongoDB Compass.


This operation uses a filter predicate of {}, which corresponds to the following SQL statement:

SELECT * FROM inventory

The following example selects from the inventory collection all documents where the status equals "D":

This operation uses a filter predicate of { status: "D" }, which corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "D"

The following example retrieves all documents from the inventory collection where status equals either "A" or "D":

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 uses a filter predicate of { status: { $in: [ "A", "D" ] } }, which corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status in ("A", "D")

Refer to the Query and Projection Operators document for the complete list of MongoDB query operators.

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:

The operation uses a filter predicate of { status: "A", qty: { $lt: 30 } }, which corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "A" AND qty < 30

See comparison operators for other MongoDB comparison operators.

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:

The operation uses a filter predicate of { $or: [ { status: 'A' }, { qty: { $lt: 30 } } ] }, which corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "A" OR qty < 30

Note

Queries that use comparison operators are subject to Type Bracketing.

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:

The operation uses a filter predicate of:

{
status: 'A',
$or: [
{ qty: { $lt: 30 } }, { item: { $regex: '^p' } }
]
}

which corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")

Note

MongoDB supports regular expressions $regex queries to perform string pattern matches.

The example in this section uses the sample movies dataset. To learn how to load the sample dataset into your MongoDB Atlas deployment, see Load Sample Data.

To project fields to return from a query in MongoDB Atlas, follow these steps:

1
  1. In the MongoDB Atlas UI, click Database in the sidebar.

  2. For the database deployment that contains the sample data, click Browse Collections.

  3. In the left navigation pane, select the sample_mflix database.

  4. Select the movies collection.

2

Specify the query filter document in the Filter field. A query filter document uses query operators to specify search conditions.

Copy the following query filter document into the Filter search bar:

{ year: 1924 }
3

This query filter returns all documents in the sample_mflix.movies collection where the year field matches 1924.

For additional query examples, see:

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.

When you run a find operation with a MongoDB driver or mongosh, the command returns a cursor that manages query results. The query results are not returned as an array of documents.

To learn how to iterate through documents in a cursor, refer to your driver's documentation. If you are using mongosh, see Iterate a Cursor in mongosh.

← Insert Methods