Convert ObjectID to String in mongo Aggregation

后端 未结 5 1192
我在风中等你
我在风中等你 2021-02-05 12:15

I\'m in this scenario right now: I have a collection X:

{
  _id:ObjectId(\'56edbb4d5f084a51131dd4c6\'),
  userRef:ObjectId(\'56edbb4d5f084a51131dd4c6\'),
  seria         


        
5条回答
  •  粉色の甜心
    2021-02-05 12:46

    Now you can try with $toString aggregation which simply converts ObjectId to string

    db.collection.aggregate([
        { "$addFields": {
            "userRef": { "$toString": "$userRef" }
        }},
        { "$group": {
          "_id": { "$concat": ["$userRef", "-", "$serialNumber"] }
        }}
    ])
    

    You can check the output here

提交回复
热议问题