如何通过 MongoDb Shell 将字符串字段转换为对象数组内的对象 ID?

How to convert a sting field to an ObjectId inside of an array of of objects via MongoDb Shell?

所以我的 MongoDb 文档看起来像这样:

{
    "_id" : ObjectId("5f1842d238ec58056d94bbb3"),
    "itemsPurchased" : [ 
        {
            "_id" : "5e7365360665d10011756af2",
            "created" : "2020-03-19T12:27:34.108Z",
            "productLine" : "5ce8ec9df5cc72002fee0d4e",
            "price" : 1.9,
            "tax" : 7,
            "ean" : "42272120",
            "expirationDate" : "2020-06-17T12:27:34.103Z",
            "kiosk" : "5c17a3d963ca649138ec522c",
            "loadCell" : "1"
        }, 
        {
            "_id" : "5e7365360665d10011756af3",
            "created" : "2020-03-19T12:27:34.108Z",
            "productLine" : "5ce8ec9df5cc72002fee0d4e",
            "price" : 1.9,
            "tax" : 7,
            "ean" : "42272120",
            "expirationDate" : "2020-06-17T12:27:34.103Z",
            "kiosk" : "5c17a3d963ca649138ec522c",
            "loadCell" : "1"
        }
    ],
    "paymentMethod" : [],
    "type" : "purchase",
    "total" : NumberDecimal("3.8"),
    "session" : ObjectId("5f1842bf1f2028e369d945f0"),
    "orgId" : ObjectId("5cddce9a51cbb2002d636741"),
    "created" : ISODate("2020-07-22T13:44:50.973Z"),
    "updated" : ISODate("2020-07-22T13:44:50.973Z"),
    "__v" : 0
}

问题是在 itemsPurchased[] 中(由于逻辑错误)有字符串类型的 ID。这里例如 "_id" : "5e7365360665d10011756af2""productLine" : "5ce8ec9df5cc72002fee0d4e"。我如何遍历所有在 itemsPurchased[] 中具有对象的文档,该对象具有字符串而不是 ObjectId,并将它们转换为 ObjectId 类型。我尝试将 $convert 和 $toObjectId 与 $map 一起使用,但似乎无法正确使用。

db.transactions.aggregate([
  { "$match": { "itemsPurchased._id": {$type: "string"}}},
  { "$set": "itemsPurchased1": {
      "$map": {
        "input": "$itemsPurchased",
        "in": {
          "$toObjectId": [
            "$$this",
            {
              "productLine": {
                "$toObjectId": "$$this.productLine"
              }
            }
          ]
        }
      }
    }
  }

无法正确获取地图部分

你已经正确使用了$toObjectId,当你在$map中与$$this合并时,你只需要把$mergeObjects改成$mergeObjects

  {
    "$set": {
      "itemsPurchased1": {
        "$map": {
          "input": "$itemsPurchased",
          "in": {
            "$mergeObjects": ["$$this", { "productLine": { "$toObjectId": "$$this.productLine" } }]
          }
        }
      }
    }
  }

Playground


更新很多

db.transactions.updateMany({}, 
[
  {
    "$set": {
      "itemsPurchased": {
        "$map": {
          "input": "$itemsPurchased",
          "in": {
            "$mergeObjects": ["$$this", { "productLine": { "$toObjectId": "$$this.productLine" } }]
          }
        }
      }
    }
  }
])
db.getCollection('transactions').update(
{"itemsPurchased.kiosk":{$type: "string"}}, 
[{
    "$set": {
      "itemsPurchased": {
        "$map": {
          "input": "$itemsPurchased",
          "in": {
            "$mergeObjects": [
              "$$this",
              {
                "kiosk": {
                  "$toObjectId": "$$this.kiosk"
                }
              }
            ]
          }
        }
      }
    }
  }], 
{multi:true})

这是如何更新...