合并和拆分数据#
¥Merging and splitting data
本章将介绍如何合并和拆分数据,以及在哪些情况下执行这些操作会很有用。
¥In this chapter, you will learn how to merge and split data, and in what cases it might be useful to perform these operations.
合并数据#
¥Merging data
在某些情况下,你可能需要合并(组合)并处理来自不同来源的数据。
¥In some cases, you might need to merge (combine) and process data from different sources.
数据合并可能涉及:
¥Merging data can involve:
- 从多个来源创建一个数据集。
¥Creating one data set from multiple sources.
- 在多个系统之间同步数据。这可能包括删除重复数据或在一个系统中数据更改时更新另一个系统中的数据。
¥Synchronizing data between multiple systems. This could include removing duplicate data or updating data in one system when it changes in another.
One-way vs. two-way sync
在单向同步中,数据沿一个方向同步。一个系统作为唯一的数据源。当主系统中的信息发生更改时,辅助系统中的信息也会自动更改。但如果辅助系统中的信息发生更改,主系统中不会反映这些更改。
¥In a one-way sync, data is synchronized in one direction. One system serves as the single source of truth. When information changes in that main system, it automatically changes in the secondary system; but if information changes in the secondary system, the changes aren't reflected in the main system.
在双向同步中,数据在两个系统之间双向同步。当两个系统中的任一系统的信息发生更改时,另一个系统中的信息也会自动更改。
¥In a two-way sync, data is synchronized in both directions (between both systems). When information changes in either of the two systems, it automatically changes in the other one as well.
本博客教程 讲解如何在两个 CRM 系统之间进行单向和双向数据同步。
¥This blog tutorial explains how to sync data one-way and two-way between two CRMs.
在 n8n 中,你可以使用 合并节点 合并来自两个不同节点的数据,合并节点 提供了多种合并选项:
¥In n8n, you can merge data from two different nodes using the Merge node, which provides several merging options:
-
按字段合并:要求输入字段匹配
¥Merge by Fields: requires input fields to match on
请注意,“合并”>“按字段合并”需要你输入要匹配的字段。这些字段在数据源之间应包含相同的值,以便 n8n 可以正确匹配数据。在“合并”节点中,它们分别名为 Input 1 Field 和 Input 2 Field。
¥Notice that Combine > Merge by Fields requires you enter input fields to match on. These fields should contain identical values between the data sources so n8n can properly match data together. In the Merge node, they're called Input 1 Field and Input 2 Field.

Property Input in dot notation
如果你想在“合并”节点参数 Input 1 Field 和 Input 2 Field 中引用嵌套值,则需要以点号表示法(文本格式,而非表达式)输入属性键。
¥If you want to reference nested values in the Merge node parameters Input 1 Field and Input 2 Field, you need to enter the property key in dot-notation format (as text, not as an expression).
Note
你还可以在别名“Join”下找到“合并”节点。如果你熟悉 SQL 连接,这可能会更直观。
¥You can also find the Merge node under the alias Join. This might be more intuitive if you're familiar with SQL joins.
合并练习#
¥Merge Exercise
构建一个工作流,合并来自“客户数据存储”节点和“代码”节点的数据。
¥Build a workflow that merges data from the Customer Datastore node and Code node.
- 添加一个合并节点,该节点从客户数据存储节点获取
Input 1,从代码节点获取Input 2。
¥Add a Merge node that takes Input 1 from a Customer Datastore node and Input 2 from a Code node.
2. 在“客户数据存储”节点中,运行“获取所有人员”操作。
¥In the Customer Datastore node, run the operation Get All People.
3. 在“代码”节点中,创建一个包含两个对象和三个属性的数组:name、language 和 country,其中属性 country 有两个子属性 code 和 name。
¥In the Code node, create an array of two objects with three properties: name, language, and country, where the property country has two sub-properties code and name.
-
使用客户数据库中的两个字符的信息填写这些属性的值。
¥Fill out the values of these properties with the information of two characters from the Customer Database.
-
例如,Jay Gatsby 的语言是英语,国家/地区名称是美国。
¥For example, Jay Gatsby's language is English and country name is United States. 4. 在“合并”节点中,尝试不同的合并选项。
¥In the Merge node, try out different merge options.
显示解决方案
¥??? note "Show me the solution"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
循环#
¥Looping
在某些情况下,你可能需要对数组中的每个元素或每个数据项执行相同的操作(例如,向通讯录中的每个联系人发送消息)。从技术角度来说,你需要遍历数据(使用循环)。
¥In some cases, you might need to perform the same operation on each element of an array or each data item (for example sending a message to every contact in your address book). In technical terms, you need to iterate through the data (with loops).
n8n 通常会自动处理这种重复性操作,因为每个节点对每个项目只运行一次,所以你无需在工作流中构建循环。
¥n8n generally handles this repetitive processing automatically, as the nodes run once for each item, so you don't need to build loops into your workflows.
但是,某些 节点和操作的异常 要求你在工作流中构建循环。
¥However, there are some exceptions of nodes and operations that will require you to build a loop into your workflow.
要使用 在 n8n 工作流中创建循环 角色,你需要将一个节点的输出连接到前一个节点的输入,并添加一个 If 节点来检查何时停止循环。
¥To create a loop in an n8n workflow, you need to connect the output of one node to the input of a previous node, and add an If node to check when to stop the loop.
分批拆分数据#
¥Splitting data in batches
如果需要处理大量传入数据、多次执行代码节点或避免 API 速率限制,最好将数据拆分成批次(组)并逐批处理。
¥If you need to process large volumes of incoming data, execute the Code node multiple times, or avoid API rate limits, it's best to split the data into batches (groups) and process these batches.
对于这些流程,请使用 循环遍历项目节点。此节点将输入数据拆分为指定大小的批次,并在每次迭代中返回预定义的数据量。
¥For these processes, use the Loop Over Items node. This node splits input data into a specified batch size and, with each iteration, returns a predefined amount of data.
Execution of Loop Over Items node
“循环遍历项目”节点会在所有传入的项目被分成批次并传递给工作流中的下一个节点后停止执行,因此无需添加 If 节点来停止循环。
¥The Loop Over Items node stops executing after all the incoming items get divided into batches and passed on to the next node in the workflow, so it's not necessary to add an If node to stop the loop.
循环/批处理练习#
¥Loop/Batch Exercise
构建一个工作流,读取 Medium 和 dev.to 的 RSS 源。工作流应包含三个节点:
¥Build a workflow that reads the RSS feed from Medium and dev.to. The workflow should consist of three nodes:
- 一个代码节点,返回 Medium (
https://medium.com/feed/n8n-io) 和 dev.to (https://dev.to/feed/n8n) 的 RSS 源 URL。
¥A Code node that returns the URLs of the RSS feeds of Medium (https://medium.com/feed/n8n-io) and dev.to (https://dev.to/feed/n8n).
2. 一个带有 Batch Size: 1 的“循环遍历项目”节点,它接收来自代码节点和 RSS 读取节点的输入,并遍历这些项目。
¥A Loop Over Items node with Batch Size: 1, that takes in the inputs from the Code node and RSS Read node and iterates over the items.
3. 获取 Medium RSS 源 URL 的 RSS 读取节点,以表达式形式传递:{{ $json.url }}。
¥An RSS Read node that gets the URL of the Medium RSS feed, passed as an expression: {{ $json.url }}.
-
RSS 读取节点是 异常节点 类节点之一,它只处理接收到的第一个项目,因此需要循环遍历项目节点来遍历多个项目。
¥The RSS Read node is one of the exception nodes which processes only the first item it receives, so the Loop Over Items node is necessary for iterating over multiple items.
显示解决方案
¥??? note "Show me the solution"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |