1
{
    "_id" : "tenant/data/EMAIL/ENGLISH",
    "tenantId" : "tenant2",
    "channelType" : "EMAIL",

    "template" : [ 
        {
            "_id" : "1",
            "templateName" : "abc",
            "effectiveStartDate" : ISODate("2017-01-01T12:00:00.000Z"),
            "modifiedDate" : ISODate("2017-06-02T22:08:55.782Z"),
            "active" : false
        }
    ]
}

我需要更新"templateName" : "xyz"的基础上"_id" : "tenant/data/EMAIL/ENGLISH"

我已经尝试了这些查询,但没有成功

db.getCollection('data').updateOne({"_id": "tenant/data/EMAIL/ENGLISH"},
                     {$set : { "template.$.templateName" : "XYZ"}}); 

db.getCollection('data').updateOne({"_id": "tenant/data/EMAIL/ENGLISH"},

                     {$set : { "template.templateName" : "XYZ"}}); 

任何帮助将不胜感激。

4

1 回答 1

0

我已经使用positional-all运算符来更新数组。

这是查询:

db.sample.update(
  {
    "_id": "tenant/data/EMAIL/ENGLISH"
  },
  {
    $set:{
      "template.$[].templateName":"XYZ"
    }
  }
)

输出

{
        "_id" : "tenant/data/EMAIL/ENGLISH",
        "tenantId" : "tenant2",
        "channelType" : "EMAIL",
        "template" : [
                {
                        "_id" : "1",
                        "templateName" : "XYZ",
                        "effectiveStartDate" : ISODate("2017-01-01T12:00:00Z"),
                        "modifiedDate" : ISODate("2017-06-02T22:08:55.782Z"),
                        "active" : false
                }
        ]
}

希望这会有所帮助:)

于 2020-04-13T16:00:58.317 回答