Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mongo DB is not updated by id

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

_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") };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading