Skip to main content
Glama

MCP Server Boilerplate

by ricleedo

mongo-delete-document

Remove documents from MongoDB collections using query filters. Specify database, collection, and filter criteria to delete single or multiple documents as needed.

Instructions

Delete documents from a MongoDB collection

Input Schema

NameRequiredDescriptionDefault
collectionYesCollection name
databaseYesDatabase name
deleteManyNoWhether to delete multiple documents (default: false)
filterYesQuery filter to match documents to delete

Input Schema (JSON Schema)

{ "properties": { "collection": { "description": "Collection name", "type": "string" }, "database": { "description": "Database name", "type": "string" }, "deleteMany": { "description": "Whether to delete multiple documents (default: false)", "type": "boolean" }, "filter": { "additionalProperties": {}, "description": "Query filter to match documents to delete", "type": "object" } }, "required": [ "database", "collection", "filter" ], "type": "object" }

Implementation Reference

  • Handler function executes the MongoDB delete operation (deleteOne or deleteMany) based on the provided filter and optional deleteMany flag, returning the count of deleted documents.
    async ({ database: dbName, collection: collectionName, filter, deleteMany = false }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const result = deleteMany ? await collection.deleteMany(filter) : await collection.deleteOne(filter); return { content: [ { type: "text", text: `Delete operation completed. Deleted ${result.deletedCount} document(s)`, }, ], }; } catch (error) { throw new Error(`Failed to delete document(s): ${error instanceof Error ? error.message : 'Unknown error'}`); } }
  • Zod schema defining input parameters: database name, collection name, filter object, and optional boolean for multi-delete.
    { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), filter: z.record(z.any()).describe("Query filter to match documents to delete"), deleteMany: z.boolean().optional().describe("Whether to delete multiple documents (default: false)"), },
  • src/index.ts:201-231 (registration)
    Full registration of the mongo-delete-document tool via McpServer.tool(), including name, description, schema, and inline handler.
    server.tool( "mongo-delete-document", "Delete documents from 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 delete"), deleteMany: z.boolean().optional().describe("Whether to delete multiple documents (default: false)"), }, async ({ database: dbName, collection: collectionName, filter, deleteMany = false }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const result = deleteMany ? await collection.deleteMany(filter) : await collection.deleteOne(filter); return { content: [ { type: "text", text: `Delete operation completed. Deleted ${result.deletedCount} document(s)`, }, ], }; } catch (error) { throw new Error(`Failed to delete document(s): ${error instanceof Error ? error.message : 'Unknown error'}`); } } );
  • Shared helper function to ensure MongoDB connection and database instance is available, used by the delete handler.
    async function ensureConnection(dbName: string): Promise<Db> { if (!mongoClient) { const uri = getMongoUri(); mongoClient = new MongoClient(uri); await mongoClient.connect(); } if (!databases.has(dbName)) { databases.set(dbName, mongoClient.db(dbName)); } return databases.get(dbName)!; }

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/ricleedo/mongo-boilerplate-mcp'

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