Skip to main content
Glama
CaptainCrouton89

MCP Server Boilerplate

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

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

Implementation Reference

  • The async handler function that connects to the MongoDB database, gets the collection, and executes deleteOne or deleteMany based on the deleteMany parameter, returning 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'}`);
      }
    }
  • Zod schema defining the input parameters: database, collection, filter (required), and optional deleteMany flag.
    {
      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 its 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'}`);
        }
      }
    );

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

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