Reputation: 446
This is my aggregation
db.getCollection("entities").aggregate([
{$match : {...omissis..},
{$project : {ancestors : 1}},
{$unwind: "$ancestors"}]);
The result is
{
"_id" : ObjectId("5b855ffb17285c29501dd801"),
"ancestors" : "5c62ef9b8521e37b80517583"
},
{
"_id" : ObjectId("5b8537d3571c4f3e3c0dcf54"),
"ancestors" : "5c75565b3e44853a18cc9d11"
}
I want to convert the ancestors string into ObjectId
.
I have done a lot of tests , my last one is adding this pipeline
{$project: {
result : {
"$let" : {
"vars" : { "id" : "$ancestors" },
"in" : ObjectId('$$id')
}
}
}
}
Every tests I made I have the same error
Error: invalid object id: length :
The ancestors string is a valid ObjectId
and I don't understand how to solve this error.
I know there are new commands with Mongodb 4.0 but this project still uses 3.4.15.
Upvotes: 0
Views: 707
Reputation: 1549
I used cursor
and javascript
to achieve this. Please try this
db.getCollection("entities").aggregate([
{$match : {...omissis..},
{$project : {ancestors : 1}},
{$unwind: "$ancestors"}]).forEach(function(doc){
doc.ancestors = ObjectId(doc.ancestors)
print(doc);
})
Upvotes: 1