8
n8n 中文网amn8n.com

n8n工作流:构建具有集合名称验证和HTTP响应的MongoDB API

高级

这是一个Engineering, Multimodal AI领域的自动化工作流,包含 22 个节点。主要使用 If, Code, MongoDb, Webhook, RespondToWebhook 等节点。 创建具有输入验证和HTTP响应的安全MongoDB数据检索API

前置要求
  • MongoDB 连接字符串
  • HTTP Webhook 端点(n8n 会自动生成)
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "5BSXfSyLAsQ1XwdA",
  "meta": {
    "instanceId": "2726cca490aa82c0ebb012bdcdb3596cbc823e0d9b0e26be2ae4efed4df5b51c",
    "templateCredsSetupCompleted": true
  },
  "name": "n8n 工作流:构建具有集合名称验证和 HTTP 响应的 MongoDB API",
  "tags": [],
  "nodes": [
    {
      "id": "8c193b65-ca41-42ea-92cf-5fa29a0c79ad",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "notes": "Get Collections",
      "position": [
        0,
        80
      ],
      "webhookId": "f4d2d840-fc5e-4a32-aa53-a2f026a4a8a6",
      "parameters": {
        "path": ":nameCollection",
        "options": {
          "ignoreBots": false
        },
        "responseMode": "responseNode"
      },
      "notesInFlow": true,
      "typeVersion": 2
    },
    {
      "id": "b623d4a9-7d52-4de2-ae88-9f7f61d60cd9",
      "name": "MongoDB",
      "type": "n8n-nodes-base.mongoDb",
      "position": [
        672,
        -32
      ],
      "parameters": {
        "options": {},
        "collection": "={{ $json.collection }}"
      },
      "credentials": {
        "mongoDb": {
          "id": "smjByRgHnSMlBLZV",
          "name": "MongoDB account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "1c4b3f2f-2026-405c-a9c3-c509f0125104",
      "name": "响应Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        1120,
        -32
      ],
      "parameters": {
        "options": {},
        "respondWith": "allIncomingItems"
      },
      "typeVersion": 1.4
    },
    {
      "id": "a60a4fb3-dceb-46da-b513-67fdebcbafd5",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -784,
        -336
      ],
      "parameters": {
        "width": 2224,
        "height": 704,
        "content": "# n8n 工作流:构建具有集合名称验证和 HTTP 响应的 MongoDB API"
      },
      "typeVersion": 1
    },
    {
      "id": "bb26c6e7-7d7a-4b24-9f91-0cbe2b54ff9d",
      "name": "如果",
      "type": "n8n-nodes-base.if",
      "position": [
        448,
        80
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "82c6ed89-009d-4e1e-a325-38848aff900b",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $json.valid }}",
              "rightValue": "false"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "c6e21f82-91d9-4dbe-8ba9-76985bdcc401",
      "name": "响应代码 400",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        672,
        176
      ],
      "parameters": {
        "options": {
          "responseCode": 400
        },
        "respondWith": "json",
        "responseBody": "={\n  \"code\": 400,\n  \"message\": \"{{ $json.message }}\"\n}"
      },
      "typeVersion": 1.4
    },
    {
      "id": "22787b54-c0af-4cc4-bb17-3c452deefaf0",
      "name": "IDS 格式",
      "type": "n8n-nodes-base.code",
      "position": [
        896,
        -32
      ],
      "parameters": {
        "jsCode": "// Function to replace the '_id' key with 'id' in each object of the array\nfunction replaceIdKey(arr) {\nreturn arr.map(item => {\n// Create a shallow copy of the object to avoid mutating the original\nconst newItem = { ...item };\n// If the object has an '_id' property, replace it with 'id'\nif (newItem._id !== undefined) {\nnewItem.id = newItem._id;\ndelete newItem._id;\n}\nreturn newItem;\n});\n}\n\n// Example usage in n8n Code node:\n// Get the input array from all items\nconst inputArray = $input.all().map(i => i.json);\n// Apply the function to replace '_id' with 'id'\nconst outputArray = replaceIdKey(inputArray);\n\n// If you want to return each object as a separate item:\nreturn outputArray.map(obj => ({ json: obj }));\n\n// If you want to return the whole array as a single item, use this instead:\n// return [{ json: outputArray }];"
      },
      "typeVersion": 2
    },
    {
      "id": "3f406d6d-0fb5-4e04-9620-41cc248a84d7",
      "name": "验证模式",
      "type": "n8n-nodes-base.code",
      "position": [
        224,
        80
      ],
      "parameters": {
        "jsCode": "const name = $input.first().json.params.nameCollection;\nconst regex = /^(?!system\\.)[a-zA-Z0-9._]{1,120}$/;\n\nif (!name) {\n  return { valid: false, message: 'The name Collection cannot be empty.' };\n}\n\nif (!regex.test(name)) {\n  let message = 'The name of the collection does not comply with the rules:';\n  if (name.startsWith('system.')) {\n    message += \"You cannot start with 'system.'. \";\n  }\n  if (name.length < 1 || name.length > 120) {\n    message += 'It must be between 1 and 120 characters long. ';\n  }\n  if (/[^a-zA-Z0-9._]/.test(name)) {\n    message += 'Only letters, numbers, and underscores are allowed. (_) and point (.). ';\n  }\n  return { valid: false, message };\n}\n\nreturn { valid: true, collection: name };"
      },
      "typeVersion": 2
    },
    {
      "id": "db3c923c-adcd-4696-bc53-0e52dae63586",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        -96
      ],
      "parameters": {
        "color": 5,
        "width": 688,
        "height": 256,
        "content": "## 1. Webhook 节点 (HTTP GET)"
      },
      "typeVersion": 1
    },
    {
      "id": "4a24ca29-e8a5-4639-9709-88ceda7171bc",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        176
      ],
      "parameters": {
        "color": 5,
        "width": 688,
        "height": 336,
        "content": "## 2. 代码节点 (验证层)"
      },
      "typeVersion": 1
    },
    {
      "id": "1ed72099-8556-4ece-bb06-e7d725d08f9a",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        544
      ],
      "parameters": {
        "color": 5,
        "width": 688,
        "height": 288,
        "content": "## 3. IF 节点 (验证检查)"
      },
      "typeVersion": 1
    },
    {
      "id": "7f2fa3fc-8992-4427-86a1-94846073a9f6",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        864
      ],
      "parameters": {
        "color": 5,
        "width": 688,
        "height": 176,
        "content": "## 4. MongoDB 节点 (数据检索)"
      },
      "typeVersion": 1
    },
    {
      "id": "19cf9bdf-9dfc-43e3-b994-4abfb56e3ca3",
      "name": "便签5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        1088
      ],
      "parameters": {
        "color": 5,
        "width": 688,
        "height": 176,
        "content": "## 5. 代码节点 (数据转换)"
      },
      "typeVersion": 1
    },
    {
      "id": "2041bc0c-5feb-48ab-b85d-2c47e21dec13",
      "name": "### 需要帮助?",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        1312
      ],
      "parameters": {
        "color": 5,
        "width": 688,
        "height": 176,
        "content": "## 6. 响应 Webhook 节点 (最终响应)"
      },
      "typeVersion": 1
    },
    {
      "id": "046f1588-6405-42d7-9ca2-e6374beb674f",
      "name": "## 试试看!",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -160,
        688
      ],
      "parameters": {
        "color": 3,
        "width": 432,
        "height": 128,
        "content": "## 3.A. 响应代码 400"
      },
      "typeVersion": 1
    },
    {
      "id": "31ce47f3-062d-4efc-970a-b79c47b080c5",
      "name": "GET 模型",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -48,
        16
      ],
      "parameters": {
        "color": 4,
        "width": 198,
        "height": 240,
        "content": "## 1"
      },
      "typeVersion": 1
    },
    {
      "id": "c0f855d6-82aa-4d4e-b470-b60a5d8a8bce",
      "name": "## 1. 创建新的自定义 OpenAI 凭据",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        176,
        16
      ],
      "parameters": {
        "color": 4,
        "width": 198,
        "height": 240,
        "content": "## 2"
      },
      "typeVersion": 1
    },
    {
      "id": "c05b5364-b579-4021-8edb-33bb69e7fab0",
      "name": "便签10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        400,
        16
      ],
      "parameters": {
        "color": 4,
        "width": 198,
        "height": 240,
        "content": "## 3"
      },
      "typeVersion": 1
    },
    {
      "id": "c58c0b11-aa59-40ec-9fd5-5bb9fc6ad238",
      "name": "便签 11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        608,
        128
      ],
      "parameters": {
        "color": 4,
        "width": 198,
        "height": 176,
        "content": "## 3.A."
      },
      "typeVersion": 1
    },
    {
      "id": "8baaf596-25bb-4fb7-a3e3-a32989d1c930",
      "name": "便签12",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        608,
        -80
      ],
      "parameters": {
        "color": 4,
        "width": 214,
        "height": 192,
        "content": "## 4"
      },
      "typeVersion": 1
    },
    {
      "id": "0abd9d6f-15a2-49fb-9293-d45223c52453",
      "name": "便签 13",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        832,
        -80
      ],
      "parameters": {
        "color": 4,
        "width": 214,
        "height": 192,
        "content": "## 5"
      },
      "typeVersion": 1
    },
    {
      "id": "14f0224b-157e-4776-9cbc-1edd6d73fe09",
      "name": "便签14",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1072,
        -80
      ],
      "parameters": {
        "color": 4,
        "width": 214,
        "height": 192,
        "content": "## 6"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "a6df2e76-82ca-4802-9955-5d5ca32ff586",
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "MongoDB",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond code 400",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MongoDB": {
      "main": [
        [
          {
            "node": "IDS format",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Validate Pattern",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IDS format": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate Pattern": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 工程, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量22
分类2
节点类型6
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者
Samuel Heredia

Samuel Heredia

@sheredia

I like to believe that I can contribute to creating a better world with the developments I make :)

外部链接
在 n8n.io 查看

分享此工作流