MongoDB aggregation project string to ObjectId

2020-02-26 07:39发布

I am attempting to convert a hexadecimal string to its equivalent ObjectID in an aggregation query. I tried two different methods:

db.omvas.aggregate([
    {$project:{
        EID:{$let: {
               vars: {
                  id:  "$EID"
               },
               in: ObjectId("$$id")
            }},
        }
    },
    {$group:{
        _id:"$EID"
        }
    }
]);

and

db.omvas.aggregate([
    {$project:{
        EID: ObjectId("$EID")
        }
    },
    {$group:{
        _id:"$EID"
        }
    }
]);

I keep getting the error "Error: invalid object id: length" using either method. I tested adding a literal string in place of the aggregation variable and I get a result with a proper ObjectID. It seems that the string value is not being passed through to Mongo's ObjectId function but rather the variable name is being passed as a literal string.

Anyone have any idea if what I am trying to accomplish is possible? Is there some magic I am missing?

2条回答
Anthone
2楼-- · 2020-02-26 08:13

You can use shorthand $toObjectId in mongo version 4.0.

Something like

db.omvas.aggregate([
   {"$project":{"EID":{"$toObjectId":"$EID"}}
])
查看更多
孤傲高冷的网名
3楼-- · 2020-02-26 08:26

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.

查看更多
登录 后发表回答