MongoDB aggregation project string to ObjectId
You can use shorthand $toObjectId
in mongo version 4.0.
Something like
db.omvas.aggregate([
{"$project":{"EID":{"$toObjectId":"$EID"}}
])
ObjectId
is a constructor for ObjectIds in the shell. When you write something like
"EID" : { "$let" : {
"vars" : { "id" : "$EID" },
"in" : ObjectId("$$id")
} }
the mongo shell evaluates ObjectId("$$id")
before sending the aggregation request. It's just like if you called a function in Javascript like
var x = 2
var y = 4
f(x + y) // f(6)
What you need is an aggregation operator to convert a string into an ObjectId. Unfortunately, no such function exists, as of MongoDB 2.6. Why do you need to convert the string? What are you going to do with it? Maybe there is a way around the lack of a conversion operator in aggregation.