mongodb_delete
Remove documents from a MongoDB collection using a query filter, with options to delete single or multiple records, via the MCP-MongoDB-MySQL-Server.
Instructions
Delete documents from a MongoDB collection
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| filter | Yes | MongoDB query filter | |
| many | No | Delete multiple documents if true |
Input Schema (JSON Schema)
{
"properties": {
"collection": {
"description": "Collection name",
"type": "string"
},
"filter": {
"description": "MongoDB query filter",
"type": "object"
},
"many": {
"description": "Delete multiple documents if true",
"optional": true,
"type": "boolean"
}
},
"required": [
"collection",
"filter"
],
"type": "object"
}
Implementation Reference
- src/index.ts:1069-1099 (handler)The handler function that executes the mongodb_delete tool. It ensures MongoDB connection, validates inputs, performs deleteOne or deleteMany based on 'many' flag, and returns the count of deleted documents.private async handleMongoDBDelete(args: any) { await this.ensureMongoConnection(); if (!args.collection || !args.filter) { throw new McpError(ErrorCode.InvalidParams, 'Collection name and filter are required'); } try { const collection = this.mongoDB!.collection(args.collection); let result; if (args.many) { result = await collection.deleteMany(args.filter); } else { result = await collection.deleteOne(args.filter); } return { content: [ { type: 'text', text: `Successfully deleted ${result.deletedCount} documents`, }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to delete documents: ${getErrorMessage(error)}` ); }
- src/index.ts:490-512 (registration)Registration of the 'mongodb_delete' tool in the listTools response, including name, description, and input schema definition.{ name: 'mongodb_delete', description: 'Delete documents from a MongoDB collection', inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name', }, filter: { type: 'object', description: 'MongoDB query filter', }, many: { type: 'boolean', description: 'Delete multiple documents if true', optional: true } }, required: ['collection', 'filter'], }, },
- src/index.ts:493-511 (schema)Input schema for the mongodb_delete tool defining required collection and filter objects, optional many boolean.inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name', }, filter: { type: 'object', description: 'MongoDB query filter', }, many: { type: 'boolean', description: 'Delete multiple documents if true', optional: true } }, required: ['collection', 'filter'], },
- src/index.ts:562-563 (registration)Dispatcher case in CallToolRequest handler that routes 'mongodb_delete' calls to the handleMongoDBDelete method.case 'mongodb_delete': return await this.handleMongoDBDelete(request.params.arguments);