Skip to main content
Glama
yaoxiaolinglong

MCP-MongoDB-MySQL-Server

mongodb_delete

Delete documents from a MongoDB collection using query filters to remove specific data entries through the MCP-MongoDB-MySQL-Server interface.

Instructions

Delete documents from a MongoDB collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionYesCollection name
filterYesMongoDB query filter
manyNoDelete multiple documents if true

Implementation Reference

  • The handler function that executes the mongodb_delete tool. It ensures MongoDB connection, validates inputs, and performs deleteOne or deleteMany based on the 'many' parameter.
    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)}`
        );
      }
    }
  • The input schema definition for the mongodb_delete tool, specifying required collection and filter, optional many flag.
    {
      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:562-563 (registration)
    The switch case in the request handler that registers and dispatches calls to the mongodb_delete tool to its handler.
    case 'mongodb_delete':
      return await this.handleMongoDBDelete(request.params.arguments);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/yaoxiaolinglong/mcp-mongodb-mysql-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server