Query embedded documents in mongodb

Source: Internet
Author: User
Db. getCollection (mobiles ). ensureIndex ({params. name: 1, params. value: 1}); db. getCollection (mobiles ). insert ({_ id: 1, name: ME525, brand: Motorola, params: [{name: standby time, value: 200}, {name: design, va

Db. getCollection ("mobiles "). ensureIndex ({"params. name ": 1," params. value ": 1}); db. getCollection ("mobiles "). insert ({"_ id": 1, "name": "ME525", "brand": "Motorola", "params": [{"name ": "standby time", "value": 200 },{ "name": "design", "va

Db. getCollection ("mobiles"). ensureIndex ({
"Params. name": 1,
"Params. value": 1
});

Db. getCollection ("mobiles"). insert ({
"_ Id": 1,
"Name": "ME525 ",
"Brand": "Motorola ",
"Params ":[
{"Name": "standby time", "value": 200 },
{"Name": "design", "value": "straight panel "}
]
});

Db. getCollection ("mobiles"). insert ({
"_ Id": 2,
"Name": "E7 ",
"Brand": "Nokia ",
"Params ":[
{"Name": "standby time", "value": 500 },
{"Name": "design", "value": "slide "}
]
});

If you want to query the mobile phone with the active standby time greater than 100 hours, website space, and the design of the mobile phone and Hong Kong Space, You need to query as follows:

Db. getCollection ("mobiles"). find ({
"Params ":{
$ All :[
{$ ElemMatch: {"name": "standby time", "value": {$ gt: 100 }}},
{$ ElemMatch: {"name": "design", "value": "straight panel "}}
]
}
});

Note: For more information about advanced usage such as $ all, US space, and $ elemMatch, see the official documentation.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.