mongodb数组查询问题

库中有两条数据:
第一条数据:
"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"}}}
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网