原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"
}
]
}
]
}
想查询出来的结果:
{
"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"
}
]
}
]
}
请问下各位大牛db.xxx.find() 该怎么写呢
一种实现方法:利用MongoDB的Aggregate。
1、先对包含array的contents施加$unwind
2、然后利用$match,施加过滤条件
3、最后利用$project,保留所需的field
参考下方的代码
供参考。
Love MongoDB! Have Fun!
------------------------华丽的分割符--------------------------------
MongoDB中文社区线下活动缤纷,请猛戳下方:
2017华山论剑|MongoDB中文社区
三月份杭州站在即!!! 感兴趣的朋友火速报名!!!