Skip to main content
Glama

MongoDB MCP Server

Official
by mongodb-js
deleteMany.ts2.82 kB
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js"; import type { ToolArgs, OperationType } from "../../tool.js"; import { checkIndexUsage } from "../../../helpers/indexCheck.js"; import { EJSON } from "bson"; import { zEJSON } from "../../args.js"; export class DeleteManyTool extends MongoDBToolBase { public name = "delete-many"; protected description = "Removes all documents that match the filter from a MongoDB collection"; protected argsShape = { ...DbOperationArgs, filter: zEJSON() .optional() .describe( "The query filter, specifying the deletion criteria. Matches the syntax of the filter argument of db.collection.deleteMany()" ), }; public operationType: OperationType = "delete"; protected async execute({ database, collection, filter, }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { const provider = await this.ensureConnected(); // Check if delete operation uses an index if enabled if (this.config.indexCheck) { await checkIndexUsage(provider, database, collection, "deleteMany", async () => { return provider.runCommandWithCheck(database, { explain: { delete: collection, deletes: [ { q: filter || {}, limit: 0, // 0 means delete all matching documents }, ], }, verbosity: "queryPlanner", }); }); } const result = await provider.deleteMany(database, collection, filter); return { content: [ { text: `Deleted \`${result.deletedCount}\` document(s) from collection "${collection}"`, type: "text", }, ], }; } protected getConfirmationMessage({ database, collection, filter }: ToolArgs<typeof this.argsShape>): string { const filterDescription = filter && Object.keys(filter).length > 0 ? "```json\n" + `{ "filter": ${EJSON.stringify(filter)} }\n` + "```\n\n" : "- **All documents** (No filter)\n\n"; return ( `You are about to delete documents from the \`${collection}\` collection in the \`${database}\` database:\n\n` + filterDescription + "This operation will permanently remove all documents matching the filter.\n\n" + "**Do you confirm the execution of the action?**" ); } }

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/mongodb-js/mongodb-mcp-server'

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