mongodb_update
Update documents in a MongoDB collection using query filters and update operations to modify specific data entries.
Instructions
Update documents in a MongoDB collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| filter | Yes | MongoDB query filter | |
| update | Yes | MongoDB update operations | |
| many | No | Update multiple documents if true |
Implementation Reference
- src/index.ts:1036-1067 (handler)The handler function that executes the MongoDB update operation. Ensures MongoDB connection, validates arguments, performs updateOne or updateMany based on the 'many' flag, and returns the result.private async handleMongoDBUpdate(args: any) { await this.ensureMongoConnection(); if (!args.collection || !args.filter || !args.update) { throw new McpError(ErrorCode.InvalidParams, 'Collection name, filter, and update are required'); } try { const collection = this.mongoDB!.collection(args.collection); let result; if (args.many) { result = await collection.updateMany(args.filter, args.update); } else { result = await collection.updateOne(args.filter, args.update); } return { content: [ { type: 'text', text: `Successfully updated ${result.modifiedCount} documents`, }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to update documents: ${getErrorMessage(error)}` ); } }
- src/index.ts:466-488 (schema)Input schema for the mongodb_update tool, defining required properties: collection, filter, update, and optional many boolean.inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name', }, filter: { type: 'object', description: 'MongoDB query filter', }, update: { type: 'object', description: 'MongoDB update operations', }, many: { type: 'boolean', description: 'Update multiple documents if true', optional: true } }, required: ['collection', 'filter', 'update'], },
- src/index.ts:463-489 (registration)Registration of the mongodb_update tool in the list of tools returned by ListToolsRequest, including name, description, and input schema.{ name: 'mongodb_update', description: 'Update documents in a MongoDB collection', inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name', }, filter: { type: 'object', description: 'MongoDB query filter', }, update: { type: 'object', description: 'MongoDB update operations', }, many: { type: 'boolean', description: 'Update multiple documents if true', optional: true } }, required: ['collection', 'filter', 'update'], }, },
- src/index.ts:560-561 (registration)Dispatch registration in the CallToolRequest handler switch statement, routing calls to the handleMongoDBUpdate method.case 'mongodb_update': return await this.handleMongoDBUpdate(request.params.arguments);