Mongodb updateMany() not working in nodejs

111
January 15, 2022, at 10:20 PM

I have a collection called imported_products with documents as following,

{
    "_id" : ObjectId("61dd75ab9c56561c4e36ae64"),
    "shop" : "lotesting.myshopify.com",
    "shopUserData" : {
        "name" : "LoTesting",
        "email" : "test@gamil.com"
    },
    "productData" : {
        "title" : "Fiodio Rainbow Membrane Gaming Keyboard, Quiet Wired Computer Keyboard, 104 Silent Keys, 26 Anti-Ghosting Keys, Spill Resistant, Multimedia Control for PC and Desktop",
        "description" : "Easy Operation & Broad Compatibility: As a plug-and-play device, this USB wired gaming keyboard does not need any software support, serving as a great choice for PC, laptop, computer keyboard. It is compatible with Win XP / Win7 / Win8 / Win10.",
        "price" : 30.74,
        "priceWithCurrency" : "$30.74",
        "images" : [
            "https://m.media-amazon.com/images/I/51YO1+k0bIL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/510vNeFx5QL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/51XA0CmhmEL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/41Ywwi-KgiL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/51lq5qZLJdL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/51Nwf+EAaDL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/41tljLXU40L._SL500_.jpg",
            "https://m.media-amazon.com/images/I/51nVhirK6ML._SL500_.jpg",
            "https://m.media-amazon.com/images/I/51xMxfShNGL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/519oWM+mnzL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/51YO1+k0bIL._SL500_.jpg"
        ],
        "ASIN" : "B086168Y25",
        "marketPlace" : "www.amazon.com",
        "settings" : {
            sendNotification: true
        },
        "shopifyProductId" : 6755890495624,
        "productUrl" : "https://www.amazon.com/FIODIO-Comfortable-Anti-Ghosting-Resistant-Multimedia/dp/B086168Y25/ref=sr_1_1_sspa?keywords=gaming+keyboard&pd_rd_r=9a66bdd8-f451-45a7-931f-999bb794436d&pd_rd_w=EDbsr&pd_rd_wg=HKpTZ&pf_rd_p=12129333-2117-4490-9c17-6d31baf0582a&pf_rd_r=TTMA6EPTSPTT6ZVVMC2Q&qid=1641903304&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEyNEtSUTlGSDZOSEwzJmVuY3J5cHRlZElkPUEwNDE3NDI3MkROTkM4QTFOUE44VyZlbmNyeXB0ZWRBZElkPUEwMzQyOTQ4MVNYODZWTFgzN1RMUSZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU="
    },
    "created_at" : ISODate("2022-01-11T12:18:51.260Z")
}
{
    "_id" : ObjectId("61dd75bf9c56561c4e36ae65"),
    "shop" : "lotesting.myshopify.com",
    "shopUserData" : {
        "name" : "LoTesting",
        "email" : "test@gmail.com"
    },
    "productData" : {
        "title" : "Fiodio Rainbow Membrane Gaming Keyboard, Quiet Wired Computer Keyboard, 104 Silent Keys, 26 Anti-Ghosting Keys, Spill Resistant, Multimedia Control for PC and Desktop",
        "description" : "Easy Operation & Broad Compatibility: As a plug-and-play device, this USB wired gaming keyboard does not need any software support, serving as a great choice for PC, laptop, computer keyboard. It is compatible with Win XP / Win7 / Win8 / Win10.",
        "price" : 30.74,
        "priceWithCurrency" : "$30.74",
        "images" : [
            "https://m.media-amazon.com/images/I/51YO1+k0bIL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/510vNeFx5QL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/51XA0CmhmEL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/41Ywwi-KgiL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/51lq5qZLJdL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/51Nwf+EAaDL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/41tljLXU40L._SL500_.jpg",
            "https://m.media-amazon.com/images/I/51nVhirK6ML._SL500_.jpg",
            "https://m.media-amazon.com/images/I/51xMxfShNGL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/519oWM+mnzL._SL500_.jpg",
            "https://m.media-amazon.com/images/I/51YO1+k0bIL._SL500_.jpg"
        ],
        "ASIN" : "B086168Y25",
        "marketPlace" : "www.amazon.com",
        "settings" : {
            sendNotification: true
        },
        "shopifyProductId" : 6755890692232,
        "productUrl" : "https://www.amazon.com/FIODIO-Comfortable-Anti-Ghosting-Resistant-Multimedia/dp/B086168Y25/ref=sr_1_1_sspa?keywords=gaming+keyboard&pd_rd_r=9a66bdd8-f451-45a7-931f-999bb794436d&pd_rd_w=EDbsr&pd_rd_wg=HKpTZ&pf_rd_p=12129333-2117-4490-9c17-6d31baf0582a&pf_rd_r=TTMA6EPTSPTT6ZVVMC2Q&qid=1641903304&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEyNEtSUTlGSDZOSEwzJmVuY3J5cHRlZElkPUEwNDE3NDI3MkROTkM4QTFOUE44VyZlbmNyeXB0ZWRBZElkPUEwMzQyOTQ4MVNYODZWTFgzN1RMUSZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU="
    },
    "created_at" : ISODate("2022-01-11T12:19:11.391Z")
}

I am trying to update all of documents for a certain category under a collection in my mongodb. But, i cannot update. When i try the same query in mongodb shell, it works.

What i want to do is that, for a specific shop i want to update the settings object that is a nested object of producdData object in every document and for all documents that include that shop, in this case, "lotesting.myshopify.com". I want to update or create that settings object with certain key, value pairs.

I tried the follwoing.

export const updateProductsSettings = async (db, shop, settingsObj) => {
  try {
    await db.collection('imported_procucts').updateMany(
      { shop },
      {
        $set: {
          'productData.settings.automaticPriceChange':
            settingsObj.automaticPriceChange,
          'productData.settings.priceChangePercentage':
            settingsObj.priceChangePercentage,
        },
      }
    );
  } catch (err) {
    throw new Api500Error(err);
  }
};

The same query works on mongodb shell but in node.js, it cannot even match the shop. I checked, the shop that i am passing is correct but, the match count is 0 for the shop. It does not update anything but, it also does not give any error.

Answer 1

Need to pass a function in the argument of updating many Example

for(let n of obj){
   <context>.update({name: n.name}, {$set: {'age':n.age}}, {upsert: true},  
(err, doc)=>{
    if(err) console.log(err);
    console.log(doc)
 })
}
try {
   db.restaurant.updateMany(
      { violations: { $gt: 4 } },
      { $set: { "Review" : true } }
   );
} catch (e) {
   print(e);
}

for more learning https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/ check this link

Rent Charter Buses Company
READ ALSO
How to use regular expression in ObjectBox

How to use regular expression in ObjectBox

I want to implement regular expression in ObjectBox databaseSomething like -

116
Add validation colour in Javascript forms [closed]

Add validation colour in Javascript forms [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

104
Scraping Real Estate Website using Python

Scraping Real Estate Website using Python

I am trying to scrape the MLS Number, Price, and Address of real estate listings from a website using BeautifulSoup

122
sql query to search string has special characters using mysql

sql query to search string has special characters using mysql

I would like to know how to write sql query for search string that has special characters

143