mongo-update-document
Update documents in MongoDB collections using query filters and update operations. Modify single or multiple documents with JSON-based update commands for database management.
Instructions
Update documents in a MongoDB collection
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| database | Yes | Database name | |
| filter | Yes | Query filter to match documents to update | |
| update | Yes | Update operations as JSON object | |
| updateMany | No | Whether to update multiple documents (default: false) |
Input Schema (JSON Schema)
{
"properties": {
"collection": {
"description": "Collection name",
"type": "string"
},
"database": {
"description": "Database name",
"type": "string"
},
"filter": {
"additionalProperties": {},
"description": "Query filter to match documents to update",
"type": "object"
},
"update": {
"additionalProperties": {},
"description": "Update operations as JSON object",
"type": "object"
},
"updateMany": {
"description": "Whether to update multiple documents (default: false)",
"type": "boolean"
}
},
"required": [
"database",
"collection",
"filter",
"update"
],
"type": "object"
}
Implementation Reference
- src/index.ts:178-198 (handler)The handler function that performs the MongoDB document update using updateOne or updateMany.async ({ database: dbName, collection: collectionName, filter, update, updateMany = false }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const result = updateMany ? await collection.updateMany(filter, update) : await collection.updateOne(filter, update); return { content: [ { type: "text", text: `Update operation completed. Matched: ${result.matchedCount}, Modified: ${result.modifiedCount}`, }, ], }; } catch (error) { throw new Error(`Failed to update document(s): ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- src/index.ts:171-177 (schema)Zod input schema defining parameters for the tool.{ database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), filter: z.record(z.any()).describe("Query filter to match documents to update"), update: z.record(z.any()).describe("Update operations as JSON object"), updateMany: z.boolean().optional().describe("Whether to update multiple documents (default: false)"), },
- src/index.ts:168-199 (registration)Registration of the mongo-update-document tool with the MCP server.server.tool( "mongo-update-document", "Update documents in a MongoDB collection", { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), filter: z.record(z.any()).describe("Query filter to match documents to update"), update: z.record(z.any()).describe("Update operations as JSON object"), updateMany: z.boolean().optional().describe("Whether to update multiple documents (default: false)"), }, async ({ database: dbName, collection: collectionName, filter, update, updateMany = false }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const result = updateMany ? await collection.updateMany(filter, update) : await collection.updateOne(filter, update); return { content: [ { type: "text", text: `Update operation completed. Matched: ${result.matchedCount}, Modified: ${result.modifiedCount}`, }, ], }; } catch (error) { throw new Error(`Failed to update document(s): ${error instanceof Error ? error.message : 'Unknown error'}`); } } );