Original json
{
"_id" : ObjectId("58b3a8dc96fbc7cfb8287093"),
"name" : "《地下水质量标准》",
"GBNumber" : "GB/T 14848-93",
"dataEntryClerk" : "handsomeboy",
"lastModified" : "Fri Feb 27 2017 12:03:40 GMT+0800",
"contents" : [
{
"standardID" : "9527-01",
"unit" : "mg/L",
"classifications" : [
{
"level" : "I",
"upperLimit" : "0",
"lowerLimit" : "50"
},
{
"level" : "II",
"upperLimit" : "50",
"lowerLimit" : "150"
},
{
"level" : "III",
"upperLimit" : "150",
"lowerLimit" : "250"
},
{
"level" : "IV",
"upperLimit" : "250",
"lowerLimit" : "350"
},
{
"level" : "V",
"upperLimit" : "350",
"lowerLimit" : "-1"
}
]
},
{
"standardID" : "7439-89-6",
"unit" : "mg/L",
"classifications" : [
{
"level" : "I",
"upperLimit" : "0",
"lowerLimit" : "0.1"
},
{
"level" : "II",
"upperLimit" : "0.1",
"lowerLimit" : "0.2"
},
{
"level" : "III",
"upperLimit" : "0.2",
"lowerLimit" : "0.3"
},
{
"level" : "IV",
"upperLimit" : "0.3",
"lowerLimit" : "0.4"
},
{
"level" : "V",
"upperLimit" : "0.5",
"lowerLimit" : "-1"
}
]
}
]
}
The results you want to query:
{
"contents" : [
{
"standardID" : "9527-01",
"unit" : "mg/L",
"classifications" : [
{
"level" : "I",
"upperLimit" : "0",
"lowerLimit" : "50"
},
{
"level" : "II",
"upperLimit" : "50",
"lowerLimit" : "150"
},
{
"level" : "III",
"upperLimit" : "150",
"lowerLimit" : "250"
},
{
"level" : "IV",
"upperLimit" : "250",
"lowerLimit" : "350"
},
{
"level" : "V",
"upperLimit" : "350",
"lowerLimit" : "-1"
}
]
}
]
}
Could you please tell me how to write db.xxx.find()
First of all, a comment:
Ask a question and post minimizedexamples of reproducibleproblems. If you post such a long document, everyone will be tired of reading it.
What you need is actually to return specific documents in the array, not the entire array.
1 If you only need to return one element in the array
The first line is the query condition, and the second line is the filter condition. You can see that operators can also be used in filter conditions. However, this operator will only return the first element that meets the condition, and requires MongoDB version 2.2 or above. Or use the
subscript selector:$
The query conditions in the above example can also be used.$elemMatch
If you need to return multiple matching elements in an array:2
$unwind
Usecontents
as an independent document stream. For the code, see @bguo's answer.But if your array is large, this can cause performance issues.
$unwind
来把contents
3
$filter
This is a new operator in the3.2
Of course you can also use and many other methods.Query conditions. For example name
One way to implement it: Use MongoDB’s Aggregate.
1. First apply $unwind to the contents containing array
2. Then use $match to apply filtering conditions
3. Finally, use $project to retain the required fields
Refer to the code below
For reference.
Love MongoDB! Have Fun!
------------------------Gorgeous separator--------------------- ----------
MongoDB Chinese community has many offline activities, please click below:
2017 Huashan Sword Discussion|MongoDB Chinese Community
Hangzhou Station is coming in March! ! ! Interested friends please sign up quickly! ! !