Search code examples
mongodbnode-red

Update Record in MonogoDB using Node-Red


I'm new in Node-Red and MongoDB.

I need to update a document in collection using Node-Red.

Here is my code in Function node:

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

But got the below error

"Monog Error: need remove or update".

Edit:

Although the below code is working fine in Mongo shell.

db.Users.findAndModify({
                        query: { "UserKey": "mykey" },
                        update: { $set: { "Locked": false } }
                       })    

Thanks


Solution

  • findAndModify is not one of the supported methods for the mongo node, you will have to do it as a 2 stage process.

    Use Mongo In node to do a find, then update the payload and feed it into a Mongo Out node to do a store