我无法弄清楚这一点,我试着用终端执行相同的查询,并且它成功了。我应该注意,使用MongoHub时,同样的查询会返回一行,但在检查记录后没有变化。下面是mongo设置和更新操作。为什么Node + MongoDB使用addToSet更新无法更新阵列
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('help', server, {safe: true});
type = 'issues';
id = 2;
body = { comments: '64' };
db.collection(type, function(err, collection) {
collection.update({id:id}, {$addToSet: body}, {safe:true}, function(err, result) {
if (err) {
console.log('Error updating: ' + err);
res.send({'error':'An error has occurred'});
} else {
console.log('' + result + ' document(s) updated');
res.send(type);
}
});
});
//Mongo Collection Record
{ "_id" : ObjectId("511c000d994cde0d02adf1ba"),
"comments" : [
1,
2,
3 ],
"id" : 2,
"text" : "This is another issue test",
"title" : "Another Issue Test" }
任何帮助是极大的赞赏
有没有可能有多个文件'id = 2'? – JohnnyHK 2013-02-14 18:39:05