mongodb返回子文档
我使用的是3.2.3版。如何让函数只返回一个子文档,而不是返回整个文档mongodb返回子文档,mongodb,mongodb-query,Mongodb,Mongodb Query,我使用的是3.2.3版。如何让函数只返回一个子文档,而不是返回整个文档 db.test1.insert({ _id: "host1", "interfaces" : [ { "inf": "ge-10/3/5", "cfmstatus": "up" }, { "inf": "ge-7/1/2", "cfmstatus": "down" } ] }); db.test1.find({ $where:
db.test1.insert({
_id: "host1",
"interfaces" : [
{
"inf": "ge-10/3/5",
"cfmstatus": "up"
},
{
"inf": "ge-7/1/2",
"cfmstatus": "down"
}
]
});
db.test1.find({
$where: function () {
for (var index in this.interfaces)
if (this.interfaces[index].cfmstatus == 'down')
return this;
}
});
提前感谢。根据上述问题的描述,作为解决方案,请尝试执行以下mongodb查询
db.getCollection("test1").find({
interfaces: {
$elemMatch: {
cfmstatus: 'down'
}
}
}, {
'interfaces.$': 1
})
在上面的查询中,$elemMatch运算符用于将值过滤到数组元素中,位置运算符$将返回属于嵌入文档的第一个匹配子文档。使用
$elemMatch
db.test1.find( { interfaces: { $elemMatch: { cfmstatus: down } } } )
“$elemMatch documentation”尝试使用
$elemMatch
projectiondb.test1。查找({},{interfaces:{$elemMatch:{cfmstatus:“down”}}}})
可能存在的重复项。谢谢