Home > Software design >  Mongo DB is not updated by id
Mongo DB is not updated by id

Time:07-31

i'm trying to update mongo db document with mongo db driver on node js, the log show that it's updated but the data is not updated, i'm trying to get the data to get updated with id document and the document is not updated, but when i use another field as a variable to search the document, the document is updated.

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://192.168.1.200:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("monitoring");
  var myquery = { _id : "62e5e171f38e161d6e905772" };
  var newvalues = { $set: { status: "1" } };
  dbo.collection("hosts").updateOne(myquery, newvalues, function(err, res) {
    if (err) throw err;
    console.log("1 document updated");
    db.close();
  });
});

Picture of the data: enter image description here

CodePudding user response:

_id property in MongoDB is not of type String, it's of type ObjectId. Try this:

const ObjectID = require('mongodb').ObjectID;   
...
const myquery = { _id : new ObjectId("62e5e171f38e161d6e905772") };
  • Related