1.4 KiB
1.4 KiB
n8n MongoDB去重配置
方案1: JS函数 + MongoDB Upsert
JS函数节点配置
使用 scripts/n8n_minimal_news.js 的代码,只做基本处理和本批次去重。
MongoDB节点配置
- 操作类型: Update
- Collection: articles
- Update Key: title (用标题作为唯一键)
- Upsert: 启用 ✅
- Update Document:
{
"$setOnInsert": {
"id": "={{$json.id}}",
"title": "={{$json.title}}",
"published_time": "={{$json.published_time}}",
"source_url": "={{$json.source_url}}",
"created_at": "={{new Date().toISOString()}}"
},
"$set": {
"last_seen": "={{new Date().toISOString()}}"
}
}
方案2: 纯MongoDB节点去重
MongoDB节点配置
- 操作类型: Update Many
- Collection: articles
- Filter:
{"title": "={{$json.title}}"} - Upsert: 启用 ✅
- Update:
{
"$setOnInsert": {
"title": "={{$json.title}}",
"published_time": "={{$json.published_time}}",
"source_url": "={{$json.source_url}}",
"created_at": "={{new Date().toISOString()}}"
}
}
推荐方案
使用方案1,因为:
- JS函数生成连续的ID
- MongoDB只负责去重插入
- 逻辑清晰,易于调试
测试步骤
- 第一次运行:应该插入所有新文章
- 第二次运行:应该0条插入(全部跳过)
- 检查数据库:确认没有重复标题