deleteOne
Remove a single document from a MongoDB collection using a specific query to delete targeted data while maintaining database integrity.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collectionName | Yes | ||
| query | Yes |
Implementation Reference
- src/index.ts:130-149 (handler)The handler function for the 'deleteOne' tool. It connects to the MongoDB database if necessary, retrieves the specified collection, executes deleteOne with the provided query, and returns a 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-128 (schema)Zod schema defining the input parameters for the 'deleteOne' tool: collectionName (string) and query (object).{ collectionName: z.string(), query: z.object({}),
- src/index.ts:124-150 (registration)Registration of the 'deleteOne' tool on the MCP server using this.mcpServer.tool(name, inputSchema, 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 }], }; } } );