Jhon Caylog
Jhon Caylog

Reputation: 503

Node js and mongo Update non-existing data in a file but data is present in the database

I have a feature in my program where i upload file from csv and then parse it and add to the mongo database.

Scenario One , Let us say that i have data from the ff which was already parse and successfully added to the database. Now the db contains the 2 records.

File1

{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
},{
  "userId": 2,
  "id": 2,
  "title": "delectus aut autem2",
  "completed": false
}

Now the user upload file again but it only now contains the data1 with userId 1 and the data 2 with userID 2 was no longer present but userID2 is still present in the database.

So what i want is that it will update the data that is present in the file and also update data that is not present in the file but present in the database

For example if i will upload this File2 this will update userID 1 with the data below and would also update userID2 and set userID2 completed value to : true.

File2

{
      "userId": 1,
      "id": 1,
      "title": "delectus aut autem 1111",
      "completed": false


    },

Any idea what is the best way to do this ?. I have my current code below it has the same flow with the discussed problem above, I just used VIN.

 async.parallel({
                            Q1: function (next) {
                                //update existing one
                                Vehicle.model.findOne().where('VIN', value.VIN).then(function (data) {
                                    if (data) {
                                        Vehicle.updateItem(data, value, function (err) {
                                            if (err) {
                                                console.log("[ERROR]", err)
                                            }
                                        })
                                    } else {
                                        //else create new data
                                        var newVehicle = new Vehicle.model({ value }),
                                            updater = newVehicle.getUpdateHandler(value, res, {
                                                errorMessage: 'There was an error creating new data:'
                                            });
                                        updater.process(value, function (err) {
                                            if (err) {
                                                console.log("[ERROR]", err)
                                            }
                                        })
                                    }

                                })
                            },
                            Q2: function(next){
        
                            }
                        })

Upvotes: 1

Views: 245

Answers (1)

Shubham Dixit
Shubham Dixit

Reputation: 1

{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
},{
  "userId": 2,
  "id": 2,
  "title": "delectus aut autem2",
  "completed": false
}

The upsert Query.What it will do

1-If the document with userID 2 already exists ,it will update all the fields in database which are provided

2-As upsert is true ,so if document doesn't exists it will insert a new document.

 db.collection.update({userId:2},{$set:{userID:2,id:2,title:"delectus aut autem2","completed": false}}, {upsert:true})

Look at the documents

Upvotes: 1

Related Questions