库中有两条数据:
第一条数据:
"test" : [{
"id" : "2015",
"type" : 5
}, {
"id" : "2016",
"type" : 30
}, {
"id" : "2017",
"type" : 40
}]
第二条数据:
"test" : [{
"id" : "2015",
"type" : 5
}, {
"id" : "2017",
"type" : 30
}, {
"id" : "2016",
"type" : 40
}]
上面的两条数据,如何查询出id=2017且type=40的那条数据?
mongodbä¸ä½¿ç¨aggregateå¯ä»¥è¿åæ°ç»å段æ°ç»çæå®ç´¢å¼çå
ç´
åèè¯å¥ï¼
{$project:{"test":1}},
{$unwind:"$test"},
{$match:{'test.id':2017,'test.type':40}},
{$group:{_id:"$_id","defaults":{$push:"$test"}}}
åèè¯å¥ï¼
{$project:{"test":1}},
{$unwind:"$test"},
{$match:{'test.id':2017,'test.type':40}},
{$group:{_id:"$_id","defaults":{$push:"$test"}}}
温馨提示:答案为网友推荐,仅供参考