如何在 Mulesoft Anypoint Studio 中使用 ObjectId 类型的字段之一在 MongoDB 中插入文档?

How to insert a document in MongoDB with one of fields of type ObjectId in Mulesoft Anypoint Studio?

我正在尝试在 MongoDB 集合中插入一个文档,其中一个 ObjectId 类型的字段引用另一个集合中的文档。参考以下例子:

{
    "_id": ObjectId("5d9b5191cbab733354f8345b"),
    "accountBalance": 1234.0,
    "pinCounter": 3,
    "status": "ACTIVE",
    "pinNumber": "1234",
    "accountType": "CURRENT",
    "customerId": ObjectId("5d96e3bd1c9d4400005cbb23")
}

_id 字段由 MongoDB 生成,而 customerId(上例中的 5d96e3bd1c9d4400005cbb23)在请求中提供。 但是,当尝试通过附加字符串 "ObjectId(" 以上述格式映射数据时,该字段将作为字符串插入。

是不成功的,因为在这种情况下

这是用于 ObjectId 的格式:

  "_id" : {
        "$oid": "5d9b5191cbab733354f8345b"
  }

所以完整的 DW 脚本看起来像这样:

%dw 2.0
output application/json
---
{
    "_id" : {
       "$oid": "5d9b5191cbab733354f8345b"
    },
    "accountBalance": 1234.0,
    "pinCounter": 3,
    "status": "ACTIVE",
    "pinNumber": "1234",
    "accountType": "CURRENT",
    "customerId": {
       "$oid": "5d96e3bd1c9d4400005cbb23"
    }
}