mongo-delete-document
Delete documents from MongoDB collections using query filters. Specify database, collection, and filter criteria to remove single or multiple documents as needed.
Instructions
Delete documents from 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 delete | |
| deleteMany | No | Whether to delete multiple documents (default: false) |
Implementation Reference
- src/index.ts:210-230 (handler)The asynchronous handler function that connects to the MongoDB database, retrieves the specified collection, executes deleteOne or deleteMany based on the provided filter and optional deleteMany flag, and returns a text content response indicating the number 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'}`); } }
- src/index.ts:204-209 (schema)Zod schema for input validation: requires database name, collection name, and filter object; optional deleteMany boolean to control single or multiple deletion.{ 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)The server.tool() call that registers the 'mongo-delete-document' tool with the MCP server, providing name, description, input schema, and handler function.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'}`); } } );
- src/index.ts:88-100 (helper)Helper function to establish and cache MongoDB client connection and database instances, used by the tool handler to access the database.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)!; }