Docs Menu
Docs Home
/ /

Query, Filter, and Retrieve Arrays of Objects

You can use the returnScope option to set the context of the query and return arrays of objects as individual documents.

To retrieve nested objects as individual documents using returnScope, you must:

  • Index the arrays of objects as the embeddedDocuments type.

  • Define storedSource for the nested fields that you want to retrieve. MongoDB Search only returns fields defined in the storedSource.

  • Set the returnStoredSource option to true in the query.

returnScope has the following syntax in your queries:

{
$search: {
"<operator>": {
<operator-specification>,
},
"returnScope": {
"path": "<embedded-documents-field-to-retrieve>"
},
"returnStoredSource": true
}
}
{
$searchMeta: {
"returnScope": {
"path": "<embedded-documents-field-to-retrieve>"
},
"returnStoredSource": true
},
"facet": {
"<operator>": {
<operator-specification>,
},
"facets": {
<facet-definition>
}
}
}

To learn more about query syntax, see $search.

The returnScope option sets the retrieval context for the query. If you specify returnScope in your query, MongoDB Search scores, sorts, and counts each embedded document as if it were an individual document.

In your operator specification, you must specify the full path to the field that you want to query.

When you use the returnScope option, MongoDB Search only returns the fields that you configured as storedSource within the embeddedDocument. Fields outside of the embeddedDocument path (such as fields at the root level) and fields that are not configured as storedSource are not returned.

The following examples demonstrate how to use the returnScope option in queries. The examples use the sample_training.companies sample dataset. If you load the data on your cluster and create the sample index on the fields in the collection, you can try the following queries against the sample data.

1{
2 "mappings": {
3 "dynamic": false,
4 "fields": {
5 "funding_rounds": {
6 "type": "embeddedDocuments",
7 "dynamic": true,
8 "fields": {
9 "investments": {
10 "type": "embeddedDocuments",
11 "dynamic": true
12 }
13 },
14 "storedSource": {
15 "include": [
16 "round_code",
17 "raised_currency_code",
18 "raised_amount",
19 "investments.person",
20 "investments.financial_org"
21 ]
22 },
23 }
24 }
25 }
26}

The preceding index definition configures MongoDB Search to:

  • Index the funding_rounds and funding_rounds.investments fields as the embeddedDocuments type.

  • Index all the dynamically indexable fields nested in the funding_rounds and funding_rounds.investments array of objects.

  • Store the following fields on mongot:

    • funding_rounds.round_code

    • funding_rounds.raised_currency_code

    • funding_rounds.raised_amount

    • funding_rounds.investments.person

    • funding_rounds.investments.financial_org

You can use the embeddedDocument Operator to perform element-wise querying on both the funding_rounds and funding_rounds.investments fields. The following sections demonstrate some sample queries that use the returnScope option to retrieve the embeddedDocuments fields as individual documents.

Sample Document Structure from sample_training.companies Collection
{
...,
"funding_rounds": [
{
"id": <integer>,
"round_code": "<string>",
"source_url": "<string>",
"source_description": "<string>",
"raised_amount": <integer>,
"raised_currency_code": "<string>",
"funded_year": <integer>,
"funded_month": "<string>",
"funded_day": "<string>",
"investments": [
{
"company": "<string>",
"financial_org": {
"name": "<string>",
"permalink": "<string>"
},
"person": {
"first_name": "<string>",
"last_name": "<string>",
"permalink": "<string>"
}
},
...
]
},
...
],
...
}

The following sections demonstrate sample queries that use the returnScope option to retrieve fields in embeddedDocuments type fields that were stored on mongot.

Back

searchSequenceToken

On this page