MongoDB Update failing

Hey n8n community,

I’ve just realized that I am experiencing the bug that is described in the following post regarding the MongoDB Update operation: https://community.n8n.io/t/how-to-do-mongodb-update/411. Only the first record in the collection is being updated.

The final reply in the thread mentions that there are some nodes that were not created correctly and I am wondering if there have been any developments on these nodes, or if anybody has any ideas on how to get this update operation working. The thread does mention using the “Split in Batches” node, but it is not clear to me how this would be used in this situation.

Thanks!

Some further clarifications on the exact problem that I’m having: I think in the post that I referenced above, the problem seemed to be with batch updates. However, the issue that I am experiencing is with updating individual items (therefore I don’t think the “Split in Batches” node will be able to help me here).

Here is a test workflow that demonstrates the issue that I’m experiencing. It seems that the very first document added to the Mongo collection is able to be updated without issue. However, updates attempted on any subsequent documents fail.

{
  "nodes": [
	{
	  "parameters": {},
	  "name": "Start",
	  "type": "n8n-nodes-base.start",
	  "typeVersion": 1,
	  "position": [
		250,
		100
	  ]
	},
	{
	  "parameters": {
		"values": {
		  "string": [
			{
			  "name": "text",
			  "value": "updated_text"
			}
		  ]
		},
		"options": {}
	  },
	  "name": "Set",
	  "type": "n8n-nodes-base.set",
	  "typeVersion": 1,
	  "position": [
		900,
		0
	  ]
	},
	{
	  "parameters": {
		"values": {
		  "string": [
			{
			  "name": "text",
			  "value": "updated_text"
			}
		  ]
		},
		"options": {}
	  },
	  "name": "Set1",
	  "type": "n8n-nodes-base.set",
	  "typeVersion": 1,
	  "position": [
		900,
		200
	  ]
	},
	{
	  "parameters": {
		"collection": "test",
		"query": "{ \"name\": \"item1\"}"
	  },
	  "name": "Find Item1",
	  "type": "n8n-nodes-base.mongoDb",
	  "typeVersion": 1,
	  "position": [
		750,
		0
	  ],
	  "credentials": {
		"mongoDb": "Mongo"
	  }
	},
	{
	  "parameters": {
		"collection": "test",
		"query": "{ \"name\": \"item2\"}"
	  },
	  "name": "Find Item2",
	  "type": "n8n-nodes-base.mongoDb",
	  "typeVersion": 1,
	  "position": [
		750,
		200
	  ],
	  "credentials": {
		"mongoDb": "Mongo"
	  }
	},
	{
	  "parameters": {
		"functionCode": "return [{\n  json: {\n    name: \"item1\",\n    text: \"original_text\"\n  }\n}, {\n  json: {\n    name: \"item2\",\n    text: \"original_text\"\n  }\n}];"
	  },
	  "name": "Define items",
	  "type": "n8n-nodes-base.function",
	  "typeVersion": 1,
	  "position": [
		400,
		100
	  ]
	},
	{
	  "parameters": {
		"operation": "insert",
		"collection": "test",
		"fields": "name, text"
	  },
	  "name": "Insert items",
	  "type": "n8n-nodes-base.mongoDb",
	  "typeVersion": 1,
	  "position": [
		550,
		100
	  ],
	  "credentials": {
		"mongoDb": "Mongo"
	  }
	},
	{
	  "parameters": {
		"operation": "update",
		"collection": "test",
		"fields": "text"
	  },
	  "name": "This update works",
	  "type": "n8n-nodes-base.mongoDb",
	  "typeVersion": 1,
	  "position": [
		1050,
		0
	  ],
	  "credentials": {
		"mongoDb": "Mongo"
	  }
	},
	{
	  "parameters": {
		"operation": "update",
		"collection": "test",
		"fields": "text"
	  },
	  "name": "This update doesn't",
	  "type": "n8n-nodes-base.mongoDb",
	  "typeVersion": 1,
	  "position": [
		1050,
		200
	  ],
	  "credentials": {
		"mongoDb": "Mongo"
	  }
	},
	{
	  "parameters": {
		"collection": "test",
		"query": "{ \"name\": \"item1\"}"
	  },
	  "name": "Find",
	  "type": "n8n-nodes-base.mongoDb",
	  "typeVersion": 1,
	  "position": [
		1200,
		0
	  ],
	  "credentials": {
		"mongoDb": "Mongo"
	  }
	},
	{
	  "parameters": {
		"collection": "test",
		"query": "{ \"name\": \"item2\"}"
	  },
	  "name": "Find1",
	  "type": "n8n-nodes-base.mongoDb",
	  "typeVersion": 1,
	  "position": [
		1200,
		200
	  ],
	  "credentials": {
		"mongoDb": "Mongo"
	  }
	}
  ],
  "connections": {
	"Start": {
	  "main": [
		[
		  {
			"node": "Define items",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"Set": {
	  "main": [
		[
		  {
			"node": "This update works",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"Set1": {
	  "main": [
		[
		  {
			"node": "This update doesn't",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"Find Item1": {
	  "main": [
		[
		  {
			"node": "Set",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"Find Item2": {
	  "main": [
		[
		  {
			"node": "Set1",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"Define items": {
	  "main": [
		[
		  {
			"node": "Insert items",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"Insert items": {
	  "main": [
		[
		  {
			"node": "Find Item1",
			"type": "main",
			"index": 0
		  },
		  {
			"node": "Find Item2",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"This update works": {
	  "main": [
		[
		  {
			"node": "Find",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"This update doesn't": {
	  "main": [
		[
		  {
			"node": "Find1",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	}
  }
}

In this case the SplitInBatches is really not needed and would not help. If you change your example so that instead of “id”, it uses “name” in the node which does the update it works fine.

The IDs in Mongo always confused me and cause problems all the time as they sometimes seem to only work if they get converted to some special Mongo-ID. Your example actually has a mistake as it uses “id” as update key (which does not exist, what exists is “_id”). For that reason does as update key null get used and that does for some reason update the first item. But even if that gets changed is still does not work. Sadly really not a Mongo expert. If possible use another key to update than “id”.

1 Like

Thanks so much for your reply, Jan! I’ve had difficulties with Mongo IDs in the past as well, so I’m not surprised to see this being the underlying cause for my problem here. Changing the Update Key works perfectly!

Great to hear! Hope we can improve that in the future to make that easier.

1 Like