2013-08-18 55 views
0

我正在尝试更新mongodb数据库中的文档以及失败的惨状。更新文档

我使用Node.js和受该驱动程序:https://github.com/mongodb/node-mongodb-native

这里是我的代码:

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

我创建了一个successfully updated消息,但变化没有发生。

我在做什么错?

回答

0

你需要设置你的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的对象,而不是一个字符串。它成功的原因是因为没有错误,它只是没有找到任何具有该String的_id的对象。