Node.js 更新文档
我正在尝试更新mongodb数据库中的一个文档和一个失败的文件 我正在使用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
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的对象