Skip to main content
Glama
manpreet2000

MCP Database Server

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
NameRequiredDescriptionDefault
collectionNameYes
queryYes

Implementation Reference

  • 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 }],
        };
      }
    }
  • 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 }],
          };
        }
      }
    );

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/manpreet2000/mcp-database-server'

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