deleteOne
Remove a single document from a MongoDB collection using a specific query filter to delete targeted data.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collectionName | Yes | ||
| query | Yes |
Implementation Reference
- src/index.ts:130-149 (handler)Handler function that connects to MongoDB if necessary, retrieves the collection, executes deleteOne with the provided query, and returns success or error message.async ({ collectionName, query }) => { try { let db = mongodbConnection.getDb(); if (!db) { await mongodbConnection.connect(this.MONGODB_URI); db = mongodbConnection.getDb(); if (!db) throw new Error("Failed to connect to database"); } const collection = db.collection(collectionName); await collection.deleteOne(query); return { content: [{ type: "text", text: "Document deleted successfully" }], }; } catch (error) { console.error(error); return { content: [{ type: "text", text: "Error: " + error }], }; } }
- src/index.ts:126-129 (schema)Zod schema defining input parameters for deleteOne tool: collectionName (string) and query (object).{ collectionName: z.string(), query: z.object({}), },
- src/index.ts:124-150 (registration)Registration of the deleteOne tool with the MCP server using this.mcpServer.tool(name, schema, handler).this.mcpServer.tool( "deleteOne", { collectionName: z.string(), query: z.object({}), }, async ({ collectionName, query }) => { try { let db = mongodbConnection.getDb(); if (!db) { await mongodbConnection.connect(this.MONGODB_URI); db = mongodbConnection.getDb(); if (!db) throw new Error("Failed to connect to database"); } const collection = db.collection(collectionName); await collection.deleteOne(query); return { content: [{ type: "text", text: "Document deleted successfully" }], }; } catch (error) { console.error(error); return { content: [{ type: "text", text: "Error: " + error }], }; } } );