MongoDB - Operation
在 MongoDB 集合上执行 CRUD 操作和聚合管道。
对集合执行一次 MongoDB 操作(find、findOne、insertOne、insertMany、updateOne、updateMany、deleteOne、deleteMany 或 aggregate),并将驱动结果放在 result 中返回。连接失败会重试,操作本身只执行一次。
连接你的 MongoDB 实例,自动化数据库操作:查询文档、插入记录、更新字段、删除条目以及运行聚合管道。
操作:
- Find: 使用过滤条件查询多个文档。
- Find One: 获取与过滤条件匹配的单个文档。
- Insert One / Insert Many: 添加一个或多个文档。
- Update One / Update Many: 修改与过滤条件匹配的文档。
- Delete One: 删除单个文档。
- Aggregate: 运行聚合管道。
工作原理
- 解析节点配置中的表达式。
connection_id、database、collection、operation为空时从输入项读取。过滤条件依次取自query、filter、输入项的query或filter;文档依次取自update、document、输入项。空对象{}视为未设置。 - 解析连接:
connection_id中的mongodb://URI、connection-string凭据,或包含username、password、host、port(默认 27017)及可选dbName、options的basic-auth/object凭据。数据库依次取database、URI 路径、凭据的dbName。 - 以 10 秒的服务器选择超时和连接超时建立连接,仅在瞬时网络错误(ECONNREFUSED、ETIMEDOUT、MongoServerSelectionError、MongoNetworkError 等)时重试连接。
- 将字符串形式的过滤条件和文档按 JSON 解析后执行操作。updateOne / updateMany 总是发送
{ $set: <文档> };filter 为数组时,aggregate 将其作为管道使用。 - 关闭客户端;结果为
null或空数组时经empty返回{ result: [] },否则经out返回{ result }。
在工作流中
节点在工作流文件(draft_nodes[])中的写法,以及一次成功运行的 envelope。
{
"node_id": "node-find-orders",
"label": "Find open orders",
"operator_slug": "integrations.mongodb.operation",
"operator_version": "1.0.0",
"config": {
"connection_id": "pi-credential-xxxx",
"database": "sales",
"collection": "orders",
"operation": "find",
"query": {
"status": "open",
"customer_id": "{{ $json.customer_id }}"
}
}
}{
"ok": true,
"data": {
"result": [
{
"_id": "6745f2b3c1a9e43d2f0a1b7c",
"order_id": "ord_123",
"status": "open"
}
]
},
"port": "out"
}配置
-
connection_idstring 必填 -
MongoDB 实例的连接凭据。接受 `connection-string`(mongodb:// URI)或 `basic-auth`(用户名 + 密码 + 主机)类型的凭据。
-
databasestring 可选 支持表达式 -
MongoDB 数据库名称。当凭据的连接字符串未内嵌默认数据库时必填(Azure Cosmos DB for MongoDB 中很常见,其 URI 不含数据库名)。
-
collectionstring 必填 支持表达式 -
要操作的 MongoDB 集合名称。
-
operationstring 必填 -
要执行的数据库操作。
-
queryobject 可选 支持表达式 -
MongoDB 过滤文档或聚合管道数组。
-
updateobject 可选 支持表达式 -
更新操作符对象,或要插入的文档。
代码中的行为
除 schema 描述之外,实现代码对这些键的实际处理。
-
update - 在 updateOne / updateMany 中总会被包装进
$set,因此不支持$inc、$push等更新操作符,请直接传入要设置的字段。在 insertOne / insertMany 中它就是文档(insertMany 会将单个对象包装成数组)。 -
query - 字符串会按 JSON 解析;无效 JSON 会被静默替换为
{},从而匹配所有文档。执行 updateMany 或 deleteMany 前请检查过滤条件。aggregate 时必须为数组,传入对象会执行空管道。 -
operation - 除列出的值外,代码还接受
deleteMany以及别名insert(insertOne)、update(updateMany)、delete(deleteMany)。 -
filter代码会读取,但未声明 query的未声明别名;document是update的未声明别名。-
execution - 未在本节点 schema 中声明,但连接重试会读取:
retryOnFail: false表示只尝试一次;否则使用maxTries(默认 3),以waitBetweenTries作为基础退避。未声明的http_retry_max_attempts(限制在 1–5)、http_retry_backoff_ms(默认 1000,每次翻倍并附加最多 250 毫秒抖动)和http_retry_max_wait_ms(默认 20000,最大 30000)可覆盖这些设置。 -
mongo_server_selection_timeout_ms代码会读取,但未声明 - 未声明。服务器选择超时(毫秒,默认 10000)。
mongo_connect_timeout_ms以相同方式设置连接超时。两者都不限制操作本身的执行时间。
输出端口
out 默认 操作返回了数据:find / findOne / aggregate 返回文档,写操作返回驱动的写结果(acknowledged、insertedId、matchedCount、modifiedCount、deletedCount 等)。即使写操作没有匹配任何文档,也从这里输出。
{
"result": [
{
"_id": "6745f2b3c1a9e43d2f0a1b7c",
"order_id": "ord_123",
"status": "open"
}
]
}empty find 或 aggregate 没有返回文档,或 findOne 未找到任何内容。
{
"result": []
}error 校验、凭据解析、连接(重试后)或操作执行失败。
{
"error": "Server selection timed out after 10000 ms"
}在表达式中
后续节点可通过 $("MongoDB - Operation").item.json.<路径> 读取此节点输出中的内容(请把 MongoDB - Operation 换成你的节点标签)。Editor 标记工作流编辑器在 Previous Nodes 中提示的路径(输出契约);Worker 标记 worker 实际通过 out 返回的路径,依据“端口”一节所示的输出。
| 表达式 | Editor | Worker |
|---|---|---|
$("MongoDB - Operation").item.json.result结果文档或操作状态 |
错误
| 消息 | 端口 | 原因与处理 |
|---|---|---|
connection_id is required | error | 配置和输入项中都没有凭据。collection is required 与 operation is required 同理。 |
Could not resolve database name for <connection_id>. Set the 'database' config field explicitly (required for Azure Cosmos DB for MongoDB and any other connection string that does not embed a default database). | error | database、URI 路径和凭据都没有提供数据库名称。 |
Unknown operation: <operation> | error | operation 不是受支持的名称或别名。 |
Credential <connection_id> has unsupported type '<type>' for integrations.mongodb.operation. Use 'connection-string' or 'basic-auth'. | error | 本节点不支持该凭据类型。 |
Server selection timed out after <ms> ms | error | 每次尝试都未能在超时时间内连接到服务器。 |
输入契约
-
connection_idstring 必填 -
databasestring 可选 -
collectionstring 必填 -
operationstring 必填 -
queryobject 可选 -
updateobject 可选
输出契约
-
resultobject 可选 -
结果文档或操作状态
契约(JSON Schema)
与编排器声明完全一致的定义。All 是完整的 *.definition.js;可与上方各节对照,找出文档与 schema 之间的差异。发布前可用它校验节点。
{
"slug": "integrations.mongodb.operation",
"name": "MongoDB - Operation",
"category": "integrations",
"version": "1.0.0",
"description": "Execute CRUD operations and aggregation pipelines on MongoDB collections.\n\n Connect your MongoDB instance and automate database operations — query documents, insert records, update fields, delete entries, and run aggregation pipelines.\n\n **Operations:**\n - **Find:** Query multiple documents with a filter.\n - **Find One:** Retrieve a single document matching a filter.\n - **Insert One / Insert Many:** Add one or multiple documents.\n - **Update One / Update Many:** Modify documents matching a filter.\n - **Delete One:** Remove a single document.\n - **Aggregate:** Run an aggregation pipeline.",
"capabilities": [
"nosql",
"database",
"mongo",
"mongodb"
],
"group": [
{
"key": "mongodb",
"name": "MongoDB",
"icon_url": "/icons/integrations/mongodb/mongodb.svg"
}
],
"icon_url": "/icons/integrations/mongodb/mongodb.svg",
"config_schema": {
"type": "object",
"properties": {
"connection_id": {
"type": "string",
"title": "MongoDB Credential",
"description": "Connection credential for your MongoDB instance. Accepts credentials of type `connection-string` (mongodb:// URI) or `basic-auth` (username + password + host).",
"ui_widget": "connection_picker",
"ui_options": {
"credential_types": [
"connection-string",
"basic-auth"
],
"types": [
"mongo"
]
}
},
"database": {
"type": "string",
"title": "Database",
"description": "Name of the MongoDB database. Required when the credential connection string does not embed a default database (common for Azure Cosmos DB for MongoDB, which omits the db name from the URI).",
"format": "expression"
},
"collection": {
"type": "string",
"title": "Collection",
"description": "Name of the MongoDB collection to operate on.",
"format": "expression"
},
"operation": {
"type": "string",
"title": "Operation",
"description": "The database operation to perform.",
"enum": [
"find",
"findOne",
"insertOne",
"insertMany",
"updateOne",
"updateMany",
"deleteOne",
"aggregate"
]
},
"query": {
"type": "object",
"title": "Filter / Query",
"description": "MongoDB filter document or aggregation pipeline array.",
"format": "expression",
"displayOptions": {
"show": {
"operation": [
"find",
"findOne",
"updateOne",
"updateMany",
"deleteOne",
"aggregate"
]
}
}
},
"update": {
"type": "object",
"title": "Update / Document",
"description": "Update operators object or document(s) to insert.",
"format": "expression",
"displayOptions": {
"show": {
"operation": [
"insertOne",
"insertMany",
"updateOne",
"updateMany"
]
}
}
}
},
"required": [
"connection_id",
"collection",
"operation"
]
},
"input_contract_schema": {
"type": "object",
"properties": {
"connection_id": {
"type": "string"
},
"database": {
"type": "string"
},
"collection": {
"type": "string"
},
"operation": {
"type": "string"
},
"query": {
"type": "object"
},
"update": {
"type": "object"
}
},
"required": [
"connection_id",
"collection",
"operation"
]
},
"output_contract_schema": {
"type": "object",
"properties": {
"result": {
"type": "object",
"description": "Result document(s) or operation status"
}
}
},
"ports": [
{
"port_id": "out",
"label": "Result",
"description": "Operation result",
"is_default": true,
"supports_condition": true
},
{
"port_id": "empty",
"label": "Not Found",
"description": "When find/findOne returns no documents",
"is_default": false,
"supports_condition": true
},
{
"port_id": "error",
"label": "Error",
"description": "Error occurred during execution",
"is_default": false,
"supports_condition": false
}
],
"execution_mode": "async",
"required_resources": [],
"compatible_worker_pools": [
"standard-io",
"db-io"
],
"tags": [
"nosql",
"database"
],
"is_stateful": false,
"publisher": {
"identity": "org_sommatic_ai",
"profile": {
"name": "Sommatic AI",
"verified": true,
"url": "https://sommatic.ai",
"logo_url": null
}
}
}{
"type": "object",
"properties": {
"connection_id": {
"type": "string",
"title": "MongoDB Credential",
"description": "Connection credential for your MongoDB instance. Accepts credentials of type `connection-string` (mongodb:// URI) or `basic-auth` (username + password + host).",
"ui_widget": "connection_picker",
"ui_options": {
"credential_types": [
"connection-string",
"basic-auth"
],
"types": [
"mongo"
]
}
},
"database": {
"type": "string",
"title": "Database",
"description": "Name of the MongoDB database. Required when the credential connection string does not embed a default database (common for Azure Cosmos DB for MongoDB, which omits the db name from the URI).",
"format": "expression"
},
"collection": {
"type": "string",
"title": "Collection",
"description": "Name of the MongoDB collection to operate on.",
"format": "expression"
},
"operation": {
"type": "string",
"title": "Operation",
"description": "The database operation to perform.",
"enum": [
"find",
"findOne",
"insertOne",
"insertMany",
"updateOne",
"updateMany",
"deleteOne",
"aggregate"
]
},
"query": {
"type": "object",
"title": "Filter / Query",
"description": "MongoDB filter document or aggregation pipeline array.",
"format": "expression",
"displayOptions": {
"show": {
"operation": [
"find",
"findOne",
"updateOne",
"updateMany",
"deleteOne",
"aggregate"
]
}
}
},
"update": {
"type": "object",
"title": "Update / Document",
"description": "Update operators object or document(s) to insert.",
"format": "expression",
"displayOptions": {
"show": {
"operation": [
"insertOne",
"insertMany",
"updateOne",
"updateMany"
]
}
}
}
},
"required": [
"connection_id",
"collection",
"operation"
]
}{
"type": "object",
"properties": {
"connection_id": {
"type": "string"
},
"database": {
"type": "string"
},
"collection": {
"type": "string"
},
"operation": {
"type": "string"
},
"query": {
"type": "object"
},
"update": {
"type": "object"
}
},
"required": [
"connection_id",
"collection",
"operation"
]
}{
"type": "object",
"properties": {
"result": {
"type": "object",
"description": "Result document(s) or operation status"
}
}
}[
{
"port_id": "out",
"label": "Result",
"description": "Operation result",
"is_default": true,
"supports_condition": true
},
{
"port_id": "empty",
"label": "Not Found",
"description": "When find/findOne returns no documents",
"is_default": false,
"supports_condition": true
},
{
"port_id": "error",
"label": "Error",
"description": "Error occurred during execution",
"is_default": false,
"supports_condition": false
}
]执行
- 执行方式
async- 有状态
- 否
- 凭据
- 在服务器端根据
connection_id解析,密钥不会进入工作流。 - 能力
nosqldatabasemongomongodb- 标签
nosqldatabase
说明与限制
- 仅重试连接阶段,因此写操作不会被重复执行。
- find 和 aggregate 会返回所有匹配的文档,没有 limit、projection 或 sort 选项。请使用包含
$match、$project、$sort、$limit的 aggregate 管道来限制结果。 - 每次运行都会新建并关闭客户端。
已对照编排器代码核实 · 31e874b · 2026-11-26