$documents

The `$documents` aggregation pipeline stage is used to create a pipeline from a set of provided documents. This stage is particularly useful when you want to process specific documents without querying a collection.

Syntax

{
  $documents: [
    <document1>,
    <document2>,
    ...
  ]
}

Parameters

documentobjectrequired

A JSON object representing a document to include in the pipeline.

Examples

Sample Data

{
  "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
  "name": "First Up Consultants | Beverage Shop",
  "sales": { "totalSales": 75670 }
}

Create a pipeline from specific documents

Process predefined documents using the $documents stage.

This query demonstrates how to process a set of provided documents.

Query:

db.aggregate([{
  $documents: [{
    _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    name: "Lakeshore Retail | Holiday Supply Hub",
    location: { lat: 60.1441, lon: -141.5012 },
    sales: { fullSales: 3700 },
    tag: ["#ShopLocal", "#SeasonalSale"]
  }, {
    _id: "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
    name: "Contoso, Ltd. | Office Supply Deals",
    location: { lat: 40.7128, lon: -74.0060 },
    sales: { fullSales: 5400 },
    tag: ["#TechDeals", "#FreeShipping"]
  }]
}, {
  $project: {
    _id: 1,
    name: 1,
    "location.lat": 1,
    "location.lon": 1,
    "sales.fullSales": 1,
    tags: "$tag"
  }
}])

Output:

[
  {
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "name": "Lakeshore Retail | Holiday Supply Hub",
    "location": { "lat": 60.1441, "lon": -141.5012 },
    "sales": { "fullSales": 3700 },
    "tags": ["#ShopLocal", "#SeasonalSale"]
  },
  {
    "_id": "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
    "name": "Contoso, Ltd. | Office Supply Deals",
    "location": { "lat": 40.7128, "lon": -74.006 },
    "sales": { "fullSales": 5400 },
    "tags": ["#TechDeals", "#FreeShipping"]
  }
]

Combine $documents with other pipeline stages

Use $documents with $match and $sort stages.

This query filters and sorts provided documents.

Query:

db.aggregate([{
  $documents: [{
    _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    name: "Lakeshore Retail",
    sales: { fullSales: 3700 }
  }, {
    _id: "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
    name: "Contoso, Ltd.",
    sales: { fullSales: 5400 }
  }]
}, {
  $match: { "sales.fullSales": { $gt: 4000 } }
}, {
  $sort: { "sales.fullSales": -1 }
}])

Output:

[
  {
    "_id": "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
    "name": "Contoso, Ltd.",
    "sales": { "fullSales": 5400 }
  }
]

Related