Here's how to use db.collection.find() the: method for an example of a query operation on an embedded/nested document. The samples on this page use the inventory collection. To populate the inventory ( inventory ) collection to prepare some data, run the following command:

To specify equality criteria on a field that is an embedded/nested document, use the query filter document {<field>：<value>} , which <value> is the document that you want to match.

For example, the following query size selects { h: 14, w: 21, uom: "cm" } All documents that have a field equal to:

db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )

An equality match in an entire embedded document requires an exact match to the specified <value> document, including the order of the fields.For example, the following query inventory does not match any of the documents in the Inventory () collection:

db.inventory.find( { size: { w: 21, h: 14, uom: "cm" } } )

Querying nested fields

To specify a query condition on a field in an embedded/nested document, use the dot symbol (" field.nestedField ").

Specify equals match on nested fields

The following example selects size all documents that are nested in fields equal to " uomin ":

db.inventory.find( { "size.uom": "in" } )

Use the query operator to specify a match

Query filter documents can be specified by using the query operator, such as the following form of conditions:

{ <field1>: { <operator1>: <value1> }, ... }

The following query uses the sizeh less-than operator () in fields that are embedded in fields $lt :

db.inventory.find( { "size.h": { $lt: 15 } } )

Specifying and Conditions

The following query selects h All documents that have a nested field that is less than 15 , and the nested field uom equals " in " and the status field equals " D ":

This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to info-contact@alibabacloud.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.

Try 40+ Products For Free !

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.