1

i have the next document and i have to update promotion object according to promotionId executing i can update, but this query erases the other products that have no the promotion in the condition:

if i use update many i get Error: the update operation document must contain atomic operators. Please help me to get the error to update but keeping the products that does not have the promo i am updating. /* 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"
        }
    ]
}
1
  • Please edit this question with 1) Input document, 2) Condition to check prior to update, 3) result document.. Commented Apr 3, 2020 at 0:23

2 Answers 2

3

This update query updates all the nested documents promotion within the array exclusiveProducts. The value of "promotion.description" is modified with the new value "multi 2", where the "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" } ] }
)
Sign up to request clarification or add additional context in comments.

3 Comments

thank you very much that is exactly what i was looking for, just 2 questions: whats means $[pr] and arrayFilters are they functions in mongodb??
(1) The [pr] is the filtered positional operator used for updating array elements with condition specified on the element value. The name pr can be anything; it is user-defined. It represents the array element of the exclusiveProducts array which matches the condition { "exclusiveProducts.promotion.promotionId": "promo1" }.
(2) The arrayFilters and the filtered positional operator are used together; see the MongoDB documentation on Specify arrayFilters for an Array Update Operations.
2

You should use $elemMatch to find the array index and $ operator to update the data of that index accordingly

db.collection.updateOne(
    {"exclusiveProducts":{ "$elemMatch":{"promotion.promotionId":"promo1"} }},
    { $set: { "exclusiveProducts.$.promotion.description": "new description" } }
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.