2021年4月8日星期四

How to update specific field in mongoDB given conditions?

Given the following mongdoDB structure, how can i update the field isAvailable to false given that the shopName is "jamrt" and slug is "67626dae-1537-40d8-837d-483e5759ada0". This is my query but it does not work: Shop.find({ shopName: shopName}).update({products: {$elemMatch: {slug: slug}}}, { $set: { isAvailable: req.body.isAvailable} } Thanks!

"shopName": "jmart",  "products": [{          "id": 1,          "name": "Clean and Clear Deep Clean Cleanser 100g",          "slug": "8d1c895c-6911-4fc8-a34c-89c6948233d7",          "price": 4.5,          "discount_price": 0,          "category": "Health and Beauty",          "sale": false,          "subcategory": "personal care",          "color": "black",          "article": "Clean and Clear",          "quantity": 9,          "img": "https://firebasestorage.googleapis.com/v0/b/swifty-products.appspot.com/o/Jmart%2FBeauty%2FClean%20and%20Clear%20Deep%20Clean%20Cleanser%20100g.jpg?alt=media",          "vendor": {              "id": 1,              "name": "Clean and Clear"          },          "ratings": {              "star_ratings": 0,              "votes": 0          },          "isAvailable": true      }, {          "id": 2,          "name": "Colgate Total Pro Breath Health",          "slug": "67626dae-1537-40d8-837d-483e5759ada0",          "price": 4.5,          "discount_price": 0,          "category": "Health and Beauty",          "sale": false,          "subcategory": "personal care",          "color": "black",          "article": "Colgate",          "quantity": 9,          "img": "https://firebasestorage.googleapis.com/v0/b/swifty-products.appspot.com/o/Jmart%2FBeauty%2FColgate%20Total%20Pro%20Breath%20Health.jpg?alt=media",          "vendor": {              "id": 2,              "name": "Colgate"          },          "ratings": {              "star_ratings": 0,              "votes": 0          },          "isAvailable": true      },    ]  
https://stackoverflow.com/questions/67014532/how-to-update-specific-field-in-mongodb-given-conditions April 09, 2021 at 11:05AM

没有评论:

发表评论