find

The `find` command 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.

Returns documents that match a specified filter criteria in a collection.

Syntax

db.collection.find(query, projection, options)

Parameters

queryobjectrequired

A document that specifies the criteria for the documents to be retrieved.

projectionobject

(Optional) A document that specifies the fields in the matching documents to be returned in the result set.

optionsobject

(Optional) A document that specifies options for query behavior and results.

Examples

Sample Data

{
    "_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
                }
            ]
        }
    ]
}

Retrieve all documents

The find() command without any query filters returns all documents in the collection.

Query:

db.stores.find()

Output:

[
  { ...all documents in the collection... }
]

Retrieve documents with query filters

Retrieve documents using a filter on the name property.

Query:

db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"})

Output:

[
  {
    "_id": "b5c9f932-4efa-49fd-86ba-b35624d80d95",
    "name": "Fourth Coffee | Stationery Haven - New Franco",
    "location": { "lat": 13.5236, "lon": -82.5707 },
    "staff": { "totalStaff": { "fullTime": 17, "partTime": 5 } },
    "sales": { "totalSales": 35346, "salesByCategory": [ { "categoryName": "Rulers", "totalSales": 35346 } ] },
    "promotionEvents": [ ... ]
  }
]

Retrieve documents with query filters on objects

Retrieve documents using query filters on the lat and lon fields within the location object.

Query:

db.stores.find({"location.lat": 13.5236, "location.lon": -82.5707})

Output:

[
  {
    "_id": "b5c9f932-4efa-49fd-86ba-b35624d80d95",
    "location": { "lat": 13.5236, "lon": -82.5707 },
    ...
  }
]

Retrieve documents with query filters on arrays

Retrieve documents from the promotionEvents array where the eventName is "Grand Bargain Gala".

Query:

db.stores.find({"promotionEvents.eventName": "Grand Bargain Gala"})

Output:

[
  {
    "_id": "d7fe6fb9-57e8-471a-b8d2-714e3579a415",
    "promotionEvents": [
      {
        "eventName": "Grand Bargain Gala",
        ...
      }
    ]
  }
]

Retrieve documents from nested arrays

Retrieve documents from the "discounts" array, which is nested within the promotionEvents array where the categoryName is "Area Rugs".

Query:

db.stores.find({"promotionEvents.discounts.categoryName": "Area Rugs"})

Output:

[
  {
    "_id": "...",
    "promotionEvents": [
      {
        "discounts": [
          { "categoryName": "Area Rugs", ... }
        ]
      }
    ]
  }
]

Include specific fields in the response

A non-zero integer value or a boolean value of true includes the field in the response.

Query:

db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"location": 1, "sales": 1})

Output:

{
  "_id": "b5c9f932-4efa-49fd-86ba-b35624d80d95",
  "location": { "lat": 13.5236, "lon": -82.5707 },
  "sales": { "totalSales": 35346, "salesByCategory": [ { "categoryName": "Rulers", "totalSales": 35346 } ] }
}

Exclude specific fields in the response

An integer value of zero or a boolean value of false excludes the specified field from the query response.

Query:

db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"promotionEvents": 0, "location": 0, "sales": 0})

Output:

{
  "_id": "b5c9f932-4efa-49fd-86ba-b35624d80d95",
  "name": "Fourth Coffee | Stationery Haven - New Franco",
  "staff": { "totalStaff": { "fullTime": 17, "partTime": 5 } }
}

Project the first element in an array that matches the query filter criteria

The "arrayFieldName".$ command projects only the first occurrence of an object in an array that matches the specified query filters.

Query:

db.stores.find({"promotionEvents.eventName": "Grand Bargain Gala"}, {"promotionEvents.$": true})

Output:

{
  "_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 }
      ]
    }
  ]
}

Project specific elements in an array that match the query filter criteria

This query projects the eventName property and the nested Year property within the promotionEvents array.

Query:

db.stores.find({"promotionEvents.eventName": "Grand Bargain Gala"}, {"promotionEvents.eventName": true, "promotionEvents.promotionalDates.startDate.Year": true})

Output:

{
  "_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 } } }
  ]
}

Related