简体   繁体   中英

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: 在此处输入图像描述

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

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