MongoDB summary project returns _id array

As we know if we want to get the _id array, we can do:

db.collections.distinct("_id");

My question is how can I get an _id array if I need to execute complex logic using an aggregate. Example:

db.getCollection('users').aggregate({
        $match : {
            is_register_completed : { $ne : true}
        }
    }
    //other operator like $lookup, $group
    ,
     {
         $project : {_id:1}
    }
     )

I get

{
"_id" : "1",
"_id" : "2"
}

what i want is the same way we do different

{[1,2]}

Updated: this is what I'm trying to do with $ group

db.getCollection('users').aggregate({
        $match : {
            is_register_completed : { $ne : true}
        }
    },
    {
        $group: {
        _id:null, all:{$addToSet: "$_id"}
        }
     }, {$project: {_id:0,all:1}}
     )

but i still get

{
all : ["1","2"]
}

or i can do .map(function(el) { return el._id })after receiving

{
    "_id" : "1",
    "_id" : "2"
    }

but a map is a client-side transformation that I think will affect performance.

0
source share
1 answer

Edit: Quote from: How to return a string array with mongodb aggregation

.aggregate() , .

:
:

db.myCol.aggregate([
    {
        $group:{_id:null, array:{$push:"$_id"}}
    },
    {
        $project:{array:true,_id:false}
    }
])
+1

Source: https://habr.com/ru/post/1613741/


All Articles