mongo-update-document
Modify documents in a MongoDB collection by specifying database, collection, filter criteria, and update operations to apply changes to data.
Instructions
Update documents in a MongoDB collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | Database name | |
| collection | Yes | Collection 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) |
Implementation Reference
- src/index.ts:178-198 (handler)Executes the MongoDB update operation: connects to the database, gets the collection, and calls updateOne or updateMany based on the updateMany flag, returning the result counts.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)Input schema using Zod validators for the tool parameters: database, collection, filter, update, and optional updateMany.{ 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)Registers the mongo-update-document tool with the MCP server, including name, description, input schema, and handler function.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'}`); } } );