如何在upsert时使用参数键值

时间:2017-06-29 08:17:55

标签: node.js mongodb mongodb-query

var upsertDocument = function (db, table,kv,data) { 
db.collection(table).update(
{ kv },
{ $set: data },
{ upsert: true }
)
}

我可以拥有这样的代码吗?将kv(key,value)传递给函数作为参数 感谢。

upsertDocument(db,"MaxBlockSync",{idx:0},{blockNumber: 66}); 

我的输入是这样的。谢谢!

1 个答案:

答案 0 :(得分:0)

基本想法是正确的。您可以考虑使用mongoose,因此它看起来像:

`var upsertDocument = function (table, kv, data) {
    table.findOneAndUpdate(
        {_id: kv._id},
        {
            $set: {
                name: data.name
            }
        },
        {upsert: true}
    )
}`