我无法弄清楚这一点,我尝试使用终端执行相同的查询并且它成功了。我应该注意,在使用 MongoHub 时,相同的查询会返回一行,但在检查记录后没有任何变化。下面是 mongo 设置和更新操作。
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" }
任何帮助是极大的赞赏