简体   繁体   中英

How to update multiple records with multiple values in mongo db and node js?

I have few records in mongo db and i need to update all those records with different values at a single stretch which matches its "id".

The record in mongo db will not have value for "status" & "timestamp" and i need to update these. When i try to update the record, the update values are getting newly inserted into another new collection and not updating in my existing collection.

var parsedRes = [{
            "id": "1000000",
            "mobilenum": "2323442343",
            "code": "SC0",          
            "status": "1",
            "timestamp": "1602771846"
        }, {
            "id": "1000001",
            "mobilenum": "3323442343",
            "code": "SC0",           
            "status": "8",
            "timestamp": "1602771843"
        }, {
            "id": "1000002",
            "mobilenum": "4323442343",
            "code": "SC0",
            "status": "3",
            "timestamp": "1602771842"
        }, {
            "id": "1000003",
            "mobilenum": "5323442343",
            "code": "SC0",
            "status": "1",
            "timestamp": "1602771844"
        } 
    ]

    var updateData = [];

    for (var i = 0; i < parsedRes.length; i++) {

        updateData.push({
            updateOne: {
                filter: { ID: parsedRes[i].id },
                update: {
                    $set: {
                        mobile: parsedRes[i].mobilenum,
                        code: parsedRes[i].code,
                        status: parsedRes[i].status,
                        timestamp: parsedRes[i].timestamp
                    }
                }, upsert: true
            }
        });
      }
      return new Promise(function (resolve, reject) {
          mongomodel.bulkWrite(updateData, { ordered: false }, async function(err, result) {   
              if (err) {
                  console.log("errorrrrrrrr : ",err)
                  reject(err)
              } else {
                  console.log("result :   ****************** ",result)
                  resolve(result)
              }
          })
          updateData = [];
      })   
    

Could anyone please help me on where i go wrong. Is there any other approach to do this, please share. Thanks in advance.

sample document from db: @wak786 Please find the sample document

db.mongomodel.find()

{ "_id" : ObjectId("5f896c9d1148a810d90a8261"), "id" : "1000000", "status" : "1", "mobilenum" : "2323442343", "code" : "TA0","UpdateDate" : "1602771841", "createdAt" : ISODate("2020-10-16T09:49:17.693Z"), "updatedAt" : ISODate("2020-10-16T09:49:17.693Z") }
{ "_id" : ObjectId("5f896c9d1148a810d90a8262"), "id" : "1000001", "status" : "8", "mobilenum" : "3323442343", "code" : "OR0","UpdateDate" : "1602771841", "createdAt" : ISODate("2020-10-16T09:49:17.693Z"), "updatedAt" : ISODate("2020-10-16T09:49:17.693Z") }
{ "_id" : ObjectId("5f896c9d1148a810d90a8263"), "id" : "1000002", "status" : "1", "mobilenum" : "4323442343", "code" : "OC0","UpdateDate" : "1602771841", "createdAt" : ISODate("2020-10-16T09:49:17.693Z"), "updatedAt" : ISODate("2020-10-16T09:49:17.693Z") }
{ "_id" : ObjectId("5f896c9d1148a810d90a8264"), "id" : "1000003", "status" : "1", "mobilenum" : "5323442343", "code" : "TC0","UpdateDate" : "1602771841", "createdAt" : ISODate("2020-10-16T09:49:17.693Z"), "updatedAt" : ISODate("2020-10-16T09:49:17.693Z") }

Some of the things I notice here are :

  1. You are using upsert:true . It will add new documents to collection if your filter query doesn't find any matching documents. SO setting upsert:false will solve the problem of new documents getting added to collection.

  2. Another problem I suspect is in following line of code.

filter: { ID: parsedRes[i].id },

I think it should be id instead of ID (I am assuming it is not a Javascript constant). If you have const ID = "id" somewhere in your code then ignore this point.

EDIT :-

Can you try by removing $set becoz what I see in latest mongoose docs, $set is not needed anymore.

This is from mongoose docs.

https://mongoosejs.com/docs/api.html#model_Model.bulkWrite

   {
    updateOne: {
      filter: { name: 'Eddard Stark' },
      // If you were using the MongoDB driver directly, you'd need to do
      // `update: { $set: { title: ... } }` but mongoose adds $set for
      // you.
      update: { title: 'Hand of the King' }
    }
  },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM