Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 更新文档_Node.js_Mongodb - Fatal编程技术网

Node.js 更新文档

Node.js 更新文档,node.js,mongodb,Node.js,Mongodb,我正在尝试更新mongodb数据库中的一个文档和一个失败的文件 我正在使用node.js和这个驱动程序: 这是我的密码: db.collection('test').update({ _id: '5210f6bc75c7c33408000001' }, {$set: { title: "new title"}, {w:1}, function(err) { if (err) console.warn(err.message); else console.log('succ

我正在尝试更新mongodb数据库中的一个文档和一个失败的文件

我正在使用node.js和这个驱动程序:

这是我的密码:

db.collection('test').update({ _id: '5210f6bc75c7c33408000001' }, {$set: { title: "new title"}, {w:1}, function(err) {
      if (err) console.warn(err.message);
      else console.log('successfully updated');
    });
这是我的数据库:

> db.test.find()
{ "type" : "new", "title" : "my title", "desc" : [  "desc 1" ], "_id" : ObjectId("
5210f6bc75c7c33408000001") }
>
我创建了一条
成功更新的
消息,但更改没有发生


我做错了什么?

您需要将id属性设置为ObjectId

db.collection('test').update({ _id: ObjectId('5210f6bc75c7c33408000001') }, {$set: { title: "new title"}, {w:1}, function(err) {
      if (err) console.warn(err.message);
      else console.log('successfully updated');
    });
_id字段实际上是ObjectId类型的对象,而不是字符串。它之所以成功,是因为没有错误,只是找不到任何具有该字符串的_id的对象