Docs Menu

Docs HomeLaunch & Manage MongoDBMongoDB Atlas

embeddedDocument

On this page

  • Definition
  • Syntax
  • Options
  • Behavior
  • Scoring Behavior
  • Sorting Behavior
  • Highlighting
  • Limitations
  • Examples

Note

The Atlas Search embeddedDocuments index option, embeddedDocument operator, and embedded scoring option are in preview. When an Atlas Search index on a replica set or single MongoDB shard reaches Lucene's two billion document limit, Atlas Search doesn't index new documents or apply updates to existing documents for that index. A solution to accommodate this limitation will be in place when this feature is generally available. To troubleshoot any issues related to using this feature, contact Support.

embeddedDocument

The embeddedDocument operator is similar to $elemMatch operator. It constrains multiple query predicates to be satisfied from a single element of an array of embedded documents. embeddedDocument can be used only for queries over fields of the embeddedDocuments type.

embeddedDocument has the following syntax:

{
"embeddedDocument": {
"path": "<path-to-field>",
"operator": { <operator-specification> },
"score": { <score-options> }
}
}

embeddedDocument uses the following options to construct a query:

Field
Type
Description
Necessity
operator
object
Operator to use to query each document in the array of documents that you specify in the path. The moreLikeThis operator is not supported.
Required
path
string
Indexed embeddedDocuments type field to search. The specified field must be a parent for all operators and fields specified using the operator option. See Path Construction for more information.
Required
score
object
Score to assign to matching search results. You can use the embedded scoring option to configure scoring options. To learn more, see Scoring Behavior.
Optional

When you query embedded documents in arrays using the embeddedDocument operator, Atlas Search evaluates and scores the operator query predicates at different stages of query execution. Atlas Search:

  1. Evaluates each embedded document in the array independently.

  2. Combines the scores of matching results as configured using the embedded option, or scores by summing the scores of matching results if you don't specify an embedded score option.

  3. Joins the matching results with the parent document if other query predicates are specified through the compound operator.

    Note

    For string faceting, Atlas Search counts string facets once for each document in the result set. For an example of this behavior, see Examples.

By default, embeddedDocument operator uses the default aggregation strategy, sum, for combining scores of embedded document matches. The embeddedDocument operator score option allows you to override the default and configure the score of matching results using the embedded option.

Tip

See also:

To sort the parent documents by an embedded document field, you must do the following:

  • Index the parents of the embedded document child field as the document type.

  • Index the child field with string values within the embedded document as the token type. For child fields with number and date values, enable dynamic mapping to index those fields automatically.

Atlas Search sorts on parent documents only. It doesn't sort the child fields within an array of documents. For an example, see Sort Example.

You can highlight on fields if the fields are indexed under a parent field of document type for query predicates specified inside the embeddedDocument operator. For an example, see tutorial.

You can't highlight on queries inside the embeddedDocument operator.

The following examples use the sample_supplies.sales collection in the sample dataset. These examples use the following index definition on the collection:

{
"mappings": {
"dynamic": true,
"fields": {
"items": [
{
"dynamic": true,
"type": "embeddedDocuments"
},
{
"dynamic": true,
"fields": {
"tags": {
"type": "token"
}
},
"type": "document"
}
],
"purchaseMethod": {
"type": "stringFacet"
}
}
}
}
← compound
equals →