我有一个 Mongoose 模式 -
db.foo.insert({one:'a',friends:[{two:'b',three:'c'},{two:'d',three:'e'},{two:'f',three:'G'}]})
现在我想要的是两个只检索 friends
数组的 'two'
部分
那就是我想在 friends
数组中的每个对象中找到 two
的所有值的数组
这样的投影在 mongodb 中是否可能,输出看起来像 -
['b','d','f']
最佳答案
aggregate
就是你的答案
db.foo.aggregate({"$project" : {"two" : "$friends.two"}}).result
还有另一种方法(获取不同的值)
db.foo.aggregate([
{'$project': {
union:{$setUnion:["$friends.two"]}
}
}
]).result;
关于mongodb - 如何从mongodb字段中的对象数组中检索部分对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27606591/