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!
Hey @jan . I just tried this very same workflow on N8N 1.53.2 and node âMongoDB3â will fail to find the recently created document. Iâm using a Clustered Index Collection (based on auto-generated _id keys) hosted on Atlas.
Hey posting for others. What screwed me up here is that I was so used to this drag and drop functionality the google update node has, thatâs what I learned on initially. For this MongoDB update node, you need to bring in the data in with the find function, transform it with a set node, and then feed it to the update node after the set node altered the data. In that final update node, everything needs to be in the âfixedâ not expression version, and youâre literally just listing out the fields that youâve updated in your set node. Janâs example above was spot on. Hope this helps others because I just spent a morning on it.