Question regarding merge node behavior in combination with splitinbatch

Hello

I am playing with n8n and I like it a lot. I have a pretty good understanding of how the data model works, but one thing I do not understand.

Screenshot from 2022-03-04 08-39-21

In this picture we see two flows: the bottom one splits the item list, modifies one branch, and then merges it. The merge2 node is running three times, which is expected.
In the top flow the splitandbatch node, at batch size 1, runs three times. But the Merge1 node runs only once.
this I do not understand. Can anybody explain this to me please?

regards, Ernst

Hey @Ernst, sounds to me like you have accurately described the behaviour of the Merge node here. Only the first execution would trigger a merge. If you pass on two branches with multiple executions each, the Merge node would also execute multiple times:

If you want your Merge node to merge all 3 items from the single execution in input 2 and all items from each of the three executions on input 1 in your example, you could merge the data from each separate loop into a single execution first, for example like so:

Example Workflow

Thanks MutedJam
It seems that I am confusing executions with items in the workflow. My assumption is that if 1 execution on a function node that creates data like

[
{json: {foo: “bar”}},
{json: {foo: “bar”}},
{json: {foo: “bar”}}
]

It will trigger 3 executions.
But from your answer I understand that items in the workflow do not each trigger an execution.
I must say that is still a bit confusing to me, because if I for instance connect the aforementioned function to an item function, that node is triggered 3 times.

So I understand your answer and am very grateful for it, I still do not exactly understand the behavior.

I’m intrigued by this line in your example:
results = results.concat($items(“Set”, 0, i));

Is there to your knowledge more technical documentation about what the $items function is and how it works? I think that would help me to understand what’s really happening here

I found the documentation. Thanx

1 Like

Glad to hear you found :slight_smile:

Just in case some one else stumbles across this:

https://docs.n8n.io/nodes/expressions.html#method-items-nodename-string-outputindex-number-runindex-number

So the i part in my example would identify the run index. When splitting items up in n batches, a node would execute n times and this is what the run index specifies.

The 0 would be the output (most nodes only have one output, so this is pretty much the standard) and "Set" would simply be the name of the node I am taking the items from.

The Function logic to undo the SplitInBatches (in the “Merge Executions” node) is taken from this example flow and is slightly modified to return the original items rather than a transformed version: https://n8n.io/workflows/1160

And what I would like to add is that the base of my problem was that I didn’t understand the relation between items and executions: I thought each item would trigger an execution but not so: nodes get all the items and decide internally how to handle that list. Apparently this is a source of confusion for node developers as well, see the list on this page.

But now i’m aware of it it is quite clear:

  • the tick mark on the nodes after running the workflow indicate the number of executions. Opening a node with a number there allows you to select for which execution you want to see the data.
  • on the connection between nodes the number of items is displayed.

Maybe this is really obvious to everyone but it wasn’t obvious to me so perhaps it can’t hurt to spell it out…