【问题标题】:How to insert a document in MongoDB with one of fields of type ObjectId in Mulesoft Anypoint Studio?如何在 Mulesoft Anypoint Studio 中使用 ObjectId 类型的字段之一在 MongoDB 中插入文档?
【发布时间】:2019-10-14 12:46:15
【问题描述】:

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

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

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

【问题讨论】:

    标签: mongodb mule anypoint-studio mule-esb mulesoft


    【解决方案1】:

    这是用于 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"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-11
      • 2015-01-16
      • 1970-01-01
      • 2021-09-14
      • 2018-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多