Remove Duplicates(删除重复项)是n8n中的一个核心节点,用于识别和删除重复数据。它特别适用于以下场景:
该节点支持三种操作模式,可以处理单次执行内的重复项,也可以对比历次执行的数据。
Remove Duplicates节点提供三种操作模式:
Remove Items Repeated Within Current Input
适用场景:
Remove Items Processed in Previous Executions
适用场景:
Clear Deduplication History
适用场景:
| 选项 | 说明 | 示例 |
|---|---|---|
| All Fields | 对比所有字段 | 所有字段都相同才算重复 |
| All Fields Except | 排除某些字段 | 排除ID字段后相同就算重复 |
| Selected Fields | 仅对比指定字段 | 只检查name和email字段 |
使用场景:
✅ 开启:只关心去重字段,不需要其他信息
❌ 关闭:需要保留所有原始数据Value Is New
条件:项目是新的(未在历史中出现过)
适用:邮箱/ID等唯一标识
输入:{{ $json.email }}Value Is Higher than Any Previous Value
条件:当前值 > 历史最大值
适用:订单号、用户ID等递增值
输入:{{ $json.order_id }}Value Is a Date Later than Any Previous Date
条件:当前日期 > 历史最新日期
适用:时间戳、更新时间
输入:{{ $json.last_updated }}| 选项 | 说明 |
|---|---|
| Node | 去重数据仅作用于当前节点 |
| Workflow | 去重数据在整个工作流中共享 |
你有一个订阅列表,用户可能多次提交,需要去除重复的邮箱地址。
Manual Trigger → Code(生成测试数据)→ Split Out →
Remove Duplicates(去重)→ Set(字段处理)→ Webhook{
"nodes": [
{
"parameters": {},
"id": "1a2b3c4d",
"name": "Manual Trigger",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [250, 300]
},
{
"parameters": {
"mode": "runOnceForEachItem",
"jsCode": "let data = []; return { data: [\n { id: 1, email: 'user1@example.com', name: 'Alice', signup_date: '2024-09-20T10:00:00.000Z' },\n { id: 2, email: 'user2@example.com', name: 'Bob', signup_date: '2024-09-21T10:00:00.000Z' },\n { id: 3, email: 'user1@example.com', name: 'Alice', signup_date: '2024-09-20T10:00:00.000Z' },\n { id: 4, email: 'user3@example.com', name: 'Charlie', signup_date: '2024-09-22T10:00:00.000Z' },\n { id: 5, email: 'user2@example.com', name: 'Bob Smith', signup_date: '2024-09-21T11:00:00.000Z' }\n] }"
},
"id": "code-node-123",
"name": "Code - 生成测试数据",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [450, 300]
},
{
"parameters": {
"fieldToSplitOut": "data"
},
"id": "split-node-456",
"name": "Split Out - 分离数组",
"type": "n8n-nodes-base.splitOut",
"typeVersion": 1,
"position": [650, 300]
},
{
"parameters": {
"operation": "removeItemsRepeatedWithinCurrentInput",
"compare": "selectedFields",
"fieldsToCompare": ["email"],
"disableDotNotation": false,
"removeOtherFields": false
},
"id": "remove-dup-789",
"name": "Remove Duplicates - 按邮箱去重",
"type": "n8n-nodes-base.removeduplicates",
"typeVersion": 1,
"position": [850, 300]
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "field1",
"name": "duplicate_status",
"value": "=unique"
}
]
}
},
"id": "set-node-101",
"name": "Set - 添加标记",
"type": "n8n-nodes-base.set",
"typeVersion": 3,
"position": [1050, 300]
}
],
"connections": {
"Manual Trigger": {
"main": [[{ "node": "Code - 生成测试数据", "branch": 0, "type": "main" }]]
},
"Code - 生成测试数据": {
"main": [[{ "node": "Split Out - 分离数组", "branch": 0, "type": "main" }]]
},
"Split Out - 分离数组": {
"main": [[{ "node": "Remove Duplicates - 按邮箱去重", "branch": 0, "type": "main" }]]
},
"Remove Duplicates - 按邮箱去重": {
"main": [[{ "node": "Set - 添加标记", "branch": 0, "type": "main" }]]
}
}
}输入了5条记录,其中有2条重复邮箱(user1@example.com[1]和user2@example.com[2]各重复1次):
| id | name | signup_date | |
|---|---|---|---|
| 1 | user1@example.com[1] | Alice | 2024-09-20T10:00:00.000Z |
| 2 | user2@example.com[2] | Bob | 2024-09-21T10:00:00.000Z |
| 4 | user3@example.com[3] | Charlie | 2024-09-22T10:00:00.000Z |
✅ 结果:从5条减少到3条,成功去重!
Remove Items Repeated Within Current InputSelected Fieldsemail 或你要去重的字段连接到你需要的节点,如:
❌ 错误:直接输入 user.email
✅ 正确:启用 Disable Dot Notation,然后输入 user.email
❌ 错误:数组数据直接连接到Remove Duplicates
✅ 正确:先用 Split Out 分离数组成单个项
❌ 错误:测试后忘记清除历史,导致第二次运行没有新数据
✅ 正确:使用 Clear Deduplication History 清空或选择合适的 Scope
❌ 错误:需要跨执行去重,却用了 Remove Items Repeated Within Current Input
✅ 正确:跨执行去重用 Remove Items Processed in Previous Executions
有时需要按多个字段组合去重:
Fields To Compare: email,name,company这样只有email、name、company三个字段都完全相同才会被认为是重复。
{{ $json.userId }}_{{ $json.timestamp }}组合多个字段值创建唯一标识。
对于需要同时处理当前执行和历史执行的重复,可以:
Remove Items Repeated Within Current InputRemove Items Processed in Previous ExecutionsRemove Duplicates是n8n中必不可少的数据清洁工具:
| 需求 | 使用模式 |
|---|---|
| 清理单次数据中的重复 | Remove Items Repeated Within Current Input |
| 防止跨执行的重复处理 | Remove Items Processed in Previous Executions |
| 重新开始去重过程 | Clear Deduplication History |
掌握这个节点,你就能构建更加健壮、干净、高效的自动化工作流!🎉
[1] user1@example.com: mailto:user1@example.com
[2] user2@example.com: mailto:user2@example.com
[3] user3@example.com: mailto:user3@example.com
[4] 官方文档: https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.removeduplicates/
[5] n8n系列教程: https://www.undsky.com/blog/?category=n8n%E6%95%99%E7%A8%8B#