How to do MongoDb Update

What data should i feed in the input node for mongodb update operation . Can anyone provide one example how to update a field using mongodb update node ?

No Way to implement ObjectId of mongodb while updating any document based on field “_id”.

Same question here. MongoDB update doesn’t seem to work as expected. In particular when it comes to mongo ObjectIds as the update keys. Any suggestions/examples available?

Thanks

I just tested with “id” and there it seems to work fine for me.

Here my short test workflow:

Hi Jan,

thanks for that, and sorry for not elaborating about the problem.

What I experience is the update node performing the update statement on the first record only, despite the set it gets as an input.

I tried the above snippet, and it works as long as you have just one single line. As soon as I add more, I’ll get the first record updated despite what I have in the updating set.

The fact that it ignores the id (and even the _id) becomes pretty evident as the updated record comes with a new id=null field.

Here is the snippet with 4 records instead of 1:

   {
  "name": "updateTest",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        10,
        450
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "collection": "updateTest",
        "fields": "name,type"
      },
      "name": "MongoDB",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        380,
        450
      ],
      "credentials": {
        "mongoDb": "n8nSandbox"
      }
    },
    {
      "parameters": {
        "functionCode": "return [{\n  json: {\n    name: 'test001',\n    type: 'a'\n  }\n},{\n  json: {\n    name: 'test002',\n    type: 'a'\n}},{\n  json: {\n    name: 'test003',\n    type: 'b'\n}},{\n  json: {\n    name: 'test004',\n    type: 'b'\n  }\n}];"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        180,
        450
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "collection": "updateTest",
        "fields": "name"
      },
      "name": "MongoDB3",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        980,
        450
      ],
      "credentials": {
        "mongoDb": "n8nSandbox"
      }
    },
    {
      "parameters": {
        "collection": "updateTest",
        "query": "{ \"type\": \"b\" }"
      },
      "name": "MongoDB5",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        580,
        450
      ],
      "credentials": {
        "mongoDb": "n8nSandbox"
      }
    },
    {
      "parameters": {
        "collection": "updateTest",
        "query": "{ \"name\": \"newName\" }"
      },
      "name": "MongoDB6",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        1180,
        450
      ],
      "credentials": {
        "mongoDb": "n8nSandbox"
      }
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "name",
              "value": "newName"
            }
          ]
        }
      },
      "name": "Set newName",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        780,
        450
      ]
    }
  ],
  "connections": {
    "MongoDB": {
      "main": [
        [
          {
            "node": "MongoDB5",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "MongoDB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MongoDB3": {
      "main": [
        [
          {
            "node": "MongoDB6",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MongoDB5": {
      "main": [
        [
          {
            "node": "Set newName",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MongoDB6": {
      "main": [
        []
      ]
    },
    "Set newName": {
      "main": [
        [
          {
            "node": "MongoDB3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "4"
}

Thank you

Ah ok, now I understand. Yes I just checked the code. The node got created in a wrong way (like some others) and for that reason does it actually really just take the first item and ignores all the other ones. Add it to the list. We can simply not just “fix” it now, as this “fix” could also break the workflows of people. So we have planned to change all nodes which do not work correctly at the same time and then add a node-level option which allows to run nodes only once on the first item. It will still break but at least only that once for everything and not one breaking change after another for every node we have. It will also directly provide an easy fix (simply toggle the new option).

Until then the only thing you can do to make it work is to split the items with a “Split in Batches” node and so iterate over them one by one. Sorry!

1 Like