I am looking to modify one particular object within a larger JSON object using mongoDB and Node.js. Something like this:
{
"first": {
"value": "v1",
"status": "s1"
},
"second": {
"value": "v2",
"status": "s2"
},
"third": {
"value": "v3",
"status": "s3"
}
}
and I want to say replace only the middle value with something like this:
{
"second": {
"value": "v2.2",
"status": "s2.2"
}
}
At first I thought something like this:
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){
...
}
You can use the $set
operator to update a specific field like second
:
var second = { "second": {
"value": "v2.2",
"status": "s2.2"
}};
db.collection('collection').update({}, {$set: second}, function(err, updated){
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.