如我们所知,如果我们想获得一个 _id 数组,我们可以这样做:
db.collections.distinct("_id");
Run Code Online (Sandbox Code Playgroud)
我的问题是如果我需要用聚合来做一个复杂的逻辑,我怎么能得到一个 _id 数组。前任:
db.getCollection('users').aggregate({
$match : {
is_register_completed : { $ne : true}
}
}
//other operator like $lookup, $group
,
{
$project : {_id:1}
}
)
Run Code Online (Sandbox Code Playgroud)
我得到
{
"_id" : "1",
"_id" : "2"
}
Run Code Online (Sandbox Code Playgroud)
我想要的就像我们做的不同
{[1,2]}
Run Code Online (Sandbox Code Playgroud)
更新: 这就是我尝试用 $group 做的事情
db.getCollection('users').aggregate({
$match : {
is_register_completed : { $ne : true}
}
},
{
$group: {
_id:null, all:{$addToSet: "$_id"}
}
}, {$project: {_id:0,all:1}}
)
Run Code Online (Sandbox Code Playgroud)
但我仍然得到
{
all : ["1","2"]
}
Run Code Online (Sandbox Code Playgroud)
或者我可以.map(function(el) { return el._id …