4

I am using mongodb driver for nodejs.

I am getting below error while updating a record.

{"name":"MongoError","message":"selector must be a valid JavaScript object","driver":true}

Here is my script :

MongoClient.connect(url, function (err, db) {
      if (err) 
      {
        console.log('Unable to connect to the mongoDB server. Error:', err);
        return;
      }
    var collName = "bank";
    var SelectParas = {"name":"ABC"};
    var UpdateValues = {"name":"PQR"};

    db.collection(collName).update(collName,SelectParas,{$set:UpdateValues},function (err,numUpdated){
            if(err)
            {
            console.log('err');
            console.log(err);
            return; 
            }
            if(numUpdated) 
            {
             console.log('Updated Successfully %d document(s).', numUpdated);
            }
                db.close();

            });
});

I can write the below line in mongo console & it works.

db.bank.update({"name":"ABC"},{$set:{"name":"PQR"}})
1
  • 1
    Remove the collName from the update() method arguments list i.e. your operation should be db.collection(collName).update(SelectParas,{$set:UpdateValues},function (err,numUpdated){ ... }); Commented May 30, 2016 at 11:12

1 Answer 1

2

You are passing collecion name i.e. a string as find query of the update. Need not pass collecton name there.

db.collection(collName).update(collName,SelectParas,{$set:UpdateValues},function (err,numUpdated) 
// collName need not pass in the update function.

Need to use

db.collection(collName).update(SelectParas,{$set:UpdateValues},function (err,numUpdated) instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.