Home > Software design >  I am trying to create a discount price using updateMany. but when I update, there is no change in my
I am trying to create a discount price using updateMany. but when I update, there is no change in my

Time:11-02

here is my collection:

{
  "_id":"634861c7ee495492da321b2b",
  "name": "new product int",
  "price": 333,
  "description": "sdsad",
  "quantity": "1",
  "category": "test",
  "MRP": 333
},

{
  "_id": "6357a1361e40021f6f71897a",
  "name": "New Product 01",
  "price": 458,
  "description": "",
  "quantity": "5",
  "category": "test",
  "MRP": 458
}

I tried to apply a 50 percent off to all the products using the following update Many() syntax:


db.collection.updateMany({category:"test"},[{
                $set: {
                  price: {
                    $floor: {
                      $subtract: [
                        "$price",
                        {
                          $multiply: [
                            {
                              $divide: [
                                "$price",
                                100
                              ]
                            },
                            50
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            ])

but when i run this querry there is no change in my database. is there any alternate way of doing this?

which says:

acknowledged: true,
  modifiedCount: 0,
  upsertedId: null,
  upsertedCount: 0,
  matchedCount: 2

CodePudding user response:

i tried it on my end it is working. just replace the collection with the collection name e.g.

db.product.updateMany({category:"test"},[{
                $set: {
                  price: {
                    $floor: {
                      $subtract: [
                        "$price",
                        {
                          $multiply: [
                            {
                              $divide: [
                                "$price",
                                100
                              ]
                            },
                            50
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            ])```

CodePudding user response:

maybe because it is collectio instead of collection

  • Related