【问题标题】:Update specific section of data in mongoDB using Node.js使用 Node.js 更新 mongoDB 中的特定数据部分
【发布时间】:2014-06-25 05:15:46
【问题描述】:

我希望使用 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){ 
    ...
}

【问题讨论】:

    标签: json node.js mongodb


    【解决方案1】:

    您可以使用$set 运算符更新特定字段,例如second

    var second = { "second": {
        "value": "v2.2",
        "status": "s2.2"
    }};
    db.collection('collection').update({}, {$set: second}, function(err, updated){
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-12
      • 2012-06-04
      • 2023-03-04
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 2014-10-31
      • 2017-05-23
      相关资源
      最近更新 更多