| title | find |
|---|---|
| description | The find command in DocumentDB returns documents that match a specified filter criteria |
| type | commands |
| category | query-and-write |
The find command in DocumentDB is used to query documents within a collection. This command is fundamental for data retrieval operations and can be customized with filters, projections, and query options to fine-tune the results.
| Parameter | Description |
|---|---|
find |
(Required) A string specifying the collection name. |
filter |
(Optional) A document specifying the match filter; defaults to all documents. |
projection |
(Optional) A document specifying the fields or expressions to return. |
sort |
(Optional) A document specifying the result ordering. |
skip |
(Optional) A non-negative integer specifying the number of matching documents to skip. |
limit |
(Optional) An integer specifying the maximum number of results; 0 means no limit. Negative values use their absolute value, without implicitly enabling singleBatch. |
batchSize |
(Optional) A number specifying the first-batch limit, converted to a non-negative integer; defaults to 101. 0 creates an empty first batch. Response size limits still apply. |
singleBatch |
(Optional) A boolean that closes the cursor after the first batch; defaults to false. |
hint |
(Optional) A string or document specifying the index name or key pattern; also accepts a $natural hint. A non-natural hint cannot be combined with a $natural sort. |
let |
(Optional) A document specifying the command-scoped expression variables, referenced as $$name. |
maxTimeMS |
(Optional) A number specifying the execution timeout in milliseconds; 0 sets no explicit timeout. |
collation |
(Optional) A document specifying the comparison rules; requires documentdb_core.enableCollation. |
Load the airline example dataset, then run:
db.flights.find(
{ delay_minutes: { $gt: 0 } },
{ _id: 0, route_no: 1, arrival_airport: 1, delay_minutes: 1 }
).sort({ route_no: 1 }).batchSize(10).maxTimeMS(5000).toArray()The returned array is:
[
{ "route_no": "PG0102", "arrival_airport": "KZN", "delay_minutes": 12 },
{ "route_no": "PG0104", "arrival_airport": "LED", "delay_minutes": 5 }
]db.flights.find(
{ "segments.fare_conditions": "Business" },
{ _id: 0, route_no: 1 }
).sort({ route_no: 1 }).toArray()This returns route numbers PG0102 and PG0103. Dot notation selects a field inside each ticket segment; it does not require the entire segments array to equal the filter.
The shell cursor issues getMore as needed. Applications using raw wire requests must continue with the actual returned cursor.id.
db.collection.find(query, projection, options)Consider this sample document from the stores collection in the StoreData database.
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}The find() command without any query filters returns all documents in the collection.
db.stores.find()Retrieve documents using a filter on the name property.
db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"})Retrieve documents using query filters on the lat and lon fields within the location object.
db.stores.find({"location.lat": 13.5236, "location.lon": -82.5707})When the dot (.) notation isn't used to reference fields within an object, the query filter should exactly match the entire object including the order of the fields.
db.stores.find({"location": {"lat": 13.5236, "lon": -82.5707}})Retrieve documents from the promotionEvents array where the eventName is "Grand Bargain Gala".
db.stores.find({"promotionEvents.eventName": "Grand Bargain Gala"})Retrieve documents from the "discounts" array, which is nested within the promotionEvents array where the categoryName is "Area Rugs".
db.stores.find({"promotionEvents.discounts.categoryName": "Area Rugs"})The second document in the find command specifies the list of fields to project in the response.
A non-zero integer value or a boolean value of true includes the field in the response.
db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"location": 1, "sales": 1})db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"location": true, "sales": true})db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"location": 1, "sales": true})db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"location": 1, "sales": -5})All four queries are equivalent and specify the inclusion of the "location" and "sales" fields in the server response.
{
"_id": "b5c9f932-4efa-49fd-86ba-b35624d80d95",
"location": {
"lat": 13.5236,
"lon": -82.5707
},
"sales": {
"totalSales": 35346,
"salesByCategory": [
{
"categoryName": "Rulers",
"totalSales": 35346
}
]
}
}An integer value of zero or a boolean value of false excludes the specified field from the query response.
db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"promotionEvents": 0, "location": 0, "sales": 0})db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"promotionEvents": false, "location": false, "sales": false})Both queries are equivalent and return the following response:
{
"_id": "b5c9f932-4efa-49fd-86ba-b35624d80d95",
"name": "Fourth Coffee | Stationery Haven - New Franco",
"staff": {
"totalStaff": {
"fullTime": 17,
"partTime": 5
}
}
}Note
By default, the _id field is included in the server response. The projection document cannot contain both inclusion and exclusion clauses. However, the _id field is the only exception to this rule and can be excluded along with a list of fields to include and vice versa.
The "arrayFieldName".$ command projects only the first occurrence of an object in an array that matches the specified query filters.
db.stores.find({"promotionEvents.eventName": "Grand Bargain Gala"}, {"promotionEvents.$": true})One of the documents returned shows only the first element in the promotionEvents array that has the event name "Grand Bargain Gala" while excluding all other elements in the array.
{
"_id": "d7fe6fb9-57e8-471a-b8d2-714e3579a415",
"promotionEvents": [
{
"eventName": "Grand Bargain Gala",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 3,
"Day": 25
},
"endDate": {
"Year": 2024,
"Month": 4,
"Day": 1
}
},
"discounts": [
{
"categoryName": "Area Rugs",
"discountPercentage": 7
},
{
"categoryName": "Vinyl Flooring",
"discountPercentage": 12
}
]
}
]
}This query projects the eventName property and the nested Year property within the promotionEvents array.
db.stores.find({"promotionEvents.eventName": "Grand Bargain Gala"}, {"promotionEvents.eventName": true, "promotionEvents.promotionalDates.startDate.Year": true})One of the documents returned shows the specified array elements projected in the response.
{
"_id": "d7fe6fb9-57e8-471a-b8d2-714e3579a415",
"promotionEvents": [
{
"eventName": "Grand Bargain Gala",
"promotionalDates": {
"startDate": {
"Year": 2024
}
}
},
{
"eventName": "Grand Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024
}
}
},
{
"eventName": "Epic Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024
}
}
}
]
}Selects documents from a collection and returns a cursor. The backend applies the filter, projection, ordering, and result limits; the gateway packages the first batch and retains any remaining cursor state.
Source: pg_documentdb/src/aggregation/bson_aggregation_pipeline.c, ParseFindQuery. See wire protocol and common fields for shared request metadata.