使用 Node-Red 在 MongoDB 中更新记录

Update Record in MonogoDB using Node-Red

我是 Node-Red 和 MongoDB 的新手。

我需要使用 Node-Red 更新集合中的文档。

这是我在函数节点中的代码:

var newMsg = msg;
newMsg.operation  = 'findAndModify';
newMsg.payload    = {
                      query: { "UserKey": "mykey" }, 
                      update: { $set: {"Locked": false } }
                    };
return newMsg;

但出现以下错误

"Monog Error: need remove or update".

编辑:

尽管下面的代码在 Mongo shell.

中运行良好
db.Users.findAndModify({
                        query: { "UserKey": "mykey" },
                        update: { $set: { "Locked": false } }
                       })    

谢谢

findAndModify 不是 mongo 节点支持的方法之一,您必须分两步进行。

在节点中使用Mongo做一个find,然后更新payload,送入一个Mongo out节点做一个store

@phani-indra,例如,如果您有一组用户文档:

{ "chatid" : 324323,
  "alert" : false, 
  "accesslevel" : 4, 
  "_id" : ObjectId("5d76b33c6ff21e0155d035a0") 
}

并且您想将键 "alert" 更改为 true,因此您在 NodeRED 中的函数节点将类似于:

var newMsg = { 
  'collection': 'users', 
  'operation': 'update',
  'query': {
    chatid : 324323
  },
  'payload': { 
      $set: {
          alert : false
      }
   } 
}; 
return newMsg;

然后将函数节点馈入Mongo输出节点。