更新数组中的嵌套文档

问题描述 投票:0回答:1

我有下一个文档,我必须根据promotionId更新更新对象执行中,我可以更新,但是此查询将删除在以下情况下没有促销的其他产品:如果我多次使用更新,则会收到错误消息:更新操作文档必须包含原子运算符请帮助我更新错误,但保留没有促销信息的产品/ * 1 * /

db.fulFillmentCenter.update({},[
  {
    $addFields: {
      exclusiveProducts: {
        $filter: {
          input: "$exclusiveProducts",
          as: "product",
          cond: {
            $eq: [
              "$$product.promotion.promotionId",
              'promo1'
            ]
          }
        }
      }
    }
  },
  {$set: { "exclusiveProducts.promotion.description": "multi 2"}}

/ * 2 * /

{
    "_id" : 1,
    "_class" : "com.walmart.labsmx.exclusiveproducts.model.FulFillmentCenter",
    "minDateDelivery" : 9,
    "maxDateDelivery" : 2,
    "deliveryCost" : 10.0,
    "origin" : "string",
    "active" : true,
    "exclusiveProducts" : [ 
        {
            "_id" : NumberLong(1585867035599),
            "upc" : "p1",
            "totalStock" : 10,
            "availableStock" : 10,
            "price" : 0.0,
            "origin" : "debe aparece",
            "startDate" : ISODate("2020-02-10T02:47:03.472Z"),
            "endDate" : ISODate("2020-04-10T02:47:03.472Z"),
            "active" : true,
            "promotion" : {
                "_id" : NumberLong(1585867035613),
                "promotionId" : "promo1",
                "description" : "string",
                "startDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "endDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "active" : true
            }
        }, 
        {
            "_id" : NumberLong(1585867035642),
            "upc" : "p2",
            "totalStock" : 10,
            "availableStock" : 10,
            "price" : 0.0,
            "origin" : "no debe aparece",
            "startDate" : ISODate("2020-02-10T02:47:03.472Z"),
            "endDate" : ISODate("2020-04-10T02:47:03.472Z"),
            "active" : false,
            "promotion" : {
                "_id" : NumberLong(1585867035654),
                "promotionId" : "promo1",
                "description" : "string",
                "startDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "endDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "active" : true
            }
        }, 
        {
            "_id" : NumberLong(1585867035607),
            "upc" : "p3",
            "totalStock" : 10,
            "availableStock" : 10,
            "price" : 0.0,
            "origin" : "no debe aparece",
            "startDate" : ISODate("2020-02-10T02:47:03.472Z"),
            "endDate" : ISODate("2020-03-10T02:47:03.472Z"),
            "active" : true,
            "promotion" : {
                "_id" : NumberLong(1585867035668),
                "promotionId" : "promo1",
                "description" : "string",
                "startDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "endDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "active" : true
            }
        }, 
        {
            "_id" : NumberLong(1585867035644),
            "upc" : "p4",
            "totalStock" : 10,
            "availableStock" : 10,
            "price" : 0.0,
            "origin" : "debe aparece",
            "startDate" : ISODate("2020-02-10T02:47:03.472Z"),
            "endDate" : ISODate("2020-05-10T02:47:03.472Z"),
            "active" : true,
            "promotion" : {
                "_id" : NumberLong(1585867035632),
                "promotionId" : "promo1",
                "description" : "string",
                "startDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "endDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "active" : true
            }
        }, 
        {
            "_id" : NumberLong(1585867035666),
            "upc" : "p5",
            "totalStock" : 10,
            "availableStock" : 10,
            "price" : 0.0,
            "origin" : "no debe aparece",
            "startDate" : ISODate("2020-05-10T02:47:03.472Z"),
            "endDate" : ISODate("2020-07-10T02:47:03.472Z"),
            "active" : true,
            "promotion" : {
                "_id" : NumberLong(1585867035591),
                "promotionId" : "promo1",
                "description" : "string",
                "startDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "endDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "active" : true
            }
        }, 
        {
            "_id" : NumberLong(1585867035669),
            "upc" : "p20",
            "totalStock" : 10,
            "availableStock" : 10,
            "price" : 0.0,
            "origin" : "no debe aparece",
            "startDate" : ISODate("2020-05-10T02:47:03.472Z"),
            "endDate" : ISODate("2020-07-10T02:47:03.472Z"),
            "active" : true,
            "promotion" : {
                "_id" : NumberLong(1585867035661),
                "promotionId" : "promo2",
                "description" : "string",
                "startDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "endDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "active" : true
            }
        }, 
        {
            "_id" : NumberLong(1585867035643),
            "upc" : "p21",
            "totalStock" : 10,
            "availableStock" : 10,
            "price" : 0.0,
            "origin" : "no debe aparece",
            "startDate" : ISODate("2020-05-10T02:47:03.472Z"),
            "endDate" : ISODate("2020-07-10T02:47:03.472Z"),
            "active" : true,
            "promotion" : {
                "_id" : NumberLong(1585867035612),
                "promotionId" : "promo3",
                "description" : "string",
                "startDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "endDate" : ISODate("2020-04-30T17:49:27.205Z"),
                "active" : true
            }
        }
    ],
    "exclusiveStores" : [ 
        {
            "storeId" : 123,
            "origin" : "string"
        }
    ]
}
mongodb mongodb-query
1个回答
0
投票

此更新查询将更新数组promotion中的所有嵌套文档exclusiveProducts"promotion.description"的值被新值"multi 2"修改,其中"promotion.promotionId" = "promo1"

var new_desc = "multi 2";

db.collection.updateMany(
    { "exclusiveProducts.promotion.promotionId": "promo1" },
    { $set: { "exclusiveProducts.$[pr].promotion.description": new_desc } },
    { arrayFilters: [ { "pr.promotion.promotionId": "promo1" } ] }
)
© www.soinside.com 2019 - 2024. All rights reserved.