我希望使用mongoDB和Node.js修改更大的JSON对象中的一个特定对象。像这样:
{
"first": {
"value": "v1",
"status": "s1"
},
"second": {
"value": "v2",
"status": "s2"
},
"third": {
"value": "v3",
"status": "s3"
}
}
我想说只用这样的东西替换中间值:
{
"second": {
"value": "v2.2",
"status": "s2.2"
}
}
起初我想到这样的事情:
var Db = require('mongodb').Db
var db = new Db('database', new Server('localhost', 27017), {safe:true});
var second = { "second": {
"value": "v2.2",
"status": "s2.2" }
}
db.open(function(err, db){
db.collection('collection').update({}, second, {'upsert':true}, function(err, updated){
...
}
答案 0 :(得分:0)
您可以使用$set
运算符更新特定字段,例如second
:
var second = { "second": {
"value": "v2.2",
"status": "s2.2"
}};
db.collection('collection').update({}, {$set: second}, function(err, updated){