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.