Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用JS/JSON对象更新MongoDB集合?_Javascript_Node.js_Mongodb - Fatal编程技术网

Javascript 如何使用JS/JSON对象更新MongoDB集合?

Javascript 如何使用JS/JSON对象更新MongoDB集合?,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我有一个MongoDB集合,我正在寻找更新。目前,我只能在特别设置要更新的值时进行更新,例如: $set: {someVariable: someValue} 我想使用对象进行更新-例如,如果我有: data = {someVariable1: someValue, someVariable2: someValue} 我将它传递到$set参数中,它应该更新这些参数 有没有办法做到这一点?此时,如果我只是将该数据传递到$set参数中,它会添加一个额外的值,如下所示: "_id" :

我有一个MongoDB集合,我正在寻找更新。目前,我只能在特别设置要更新的值时进行更新,例如:

  $set: {someVariable: someValue}
我想使用对象进行更新-例如,如果我有:

   data = {someVariable1: someValue, someVariable2: someValue}
我将它传递到
$set
参数中,它应该更新这些参数

有没有办法做到这一点?此时,如果我只是将该数据传递到
$set
参数中,它会添加一个额外的值,如下所示:

"_id" : "xxxxxxxxxx",
"someVariable1": "someValue", 
"someVariable2": "someValue",
"data" : {
    "someVariable1": "someValue", 
    "someVariable2": "someValue"
}
以下是我的代码片段:

db.collection('test').updateOne(
        {_id: id},
        {
            $set: {
                data
            },
            $currentDate: {
                "lastModified": true
            }
        }, (err, result) => {
            assert.equal(err, null);
            callback(null, result);
        }
    )
其中数据如上所述

顺便说一下,这在Node.js中

非常感谢。

基于

您可以通过删除数据周围的括号来完成此操作:

$set: data,
$currentDate: {
    "lastModified": true
}

完美的谢谢