Skip to main content
Glama
CaptainCrouton89

MCP Server Boilerplate

mongo-update-document

Modify existing documents in MongoDB collections by specifying database, collection, filter criteria, and update operations. Supports single or multiple document updates.

Instructions

Update documents in a MongoDB collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name
collectionYesCollection name
filterYesQuery filter to match documents to update
updateYesUpdate operations as JSON object
updateManyNoWhether to update multiple documents (default: false)

Implementation Reference

  • Handler function that executes the MongoDB update operation, either updateOne or updateMany based on the updateMany flag.
    async ({ database: dbName, collection: collectionName, filter, update, updateMany = false }) => {
      try {
        const db = await ensureConnection(dbName);
        const collection: Collection = db.collection(collectionName);
        
        const result = updateMany 
          ? await collection.updateMany(filter, update)
          : await collection.updateOne(filter, update);
        
        return {
          content: [
            {
              type: "text",
              text: `Update operation completed. Matched: ${result.matchedCount}, Modified: ${result.modifiedCount}`,
            },
          ],
        };
      } catch (error) {
        throw new Error(`Failed to update document(s): ${error instanceof Error ? error.message : 'Unknown error'}`);
      }
    }
  • Zod input schema defining parameters: database, collection, filter, update, and optional updateMany.
    {
      database: z.string().describe("Database name"),
      collection: z.string().describe("Collection name"),
      filter: z.record(z.any()).describe("Query filter to match documents to update"),
      update: z.record(z.any()).describe("Update operations as JSON object"),
      updateMany: z.boolean().optional().describe("Whether to update multiple documents (default: false)"),
    },
  • src/index.ts:168-199 (registration)
    Registration of the mongo-update-document tool with McpServer.tool(), including schema and inline handler implementation.
    server.tool(
      "mongo-update-document",
      "Update documents in 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 update"),
        update: z.record(z.any()).describe("Update operations as JSON object"),
        updateMany: z.boolean().optional().describe("Whether to update multiple documents (default: false)"),
      },
      async ({ database: dbName, collection: collectionName, filter, update, updateMany = false }) => {
        try {
          const db = await ensureConnection(dbName);
          const collection: Collection = db.collection(collectionName);
          
          const result = updateMany 
            ? await collection.updateMany(filter, update)
            : await collection.updateOne(filter, update);
          
          return {
            content: [
              {
                type: "text",
                text: `Update operation completed. Matched: ${result.matchedCount}, Modified: ${result.modifiedCount}`,
              },
            ],
          };
        } catch (error) {
          throw new Error(`Failed to update document(s): ${error instanceof Error ? error.message : 'Unknown error'}`);
        }
      }
    );
  • Helper function to establish and cache MongoDB connection and database instance, used by the handler.
    async function ensureConnection(dbName: string): Promise<Db> {
      if (!mongoClient) {
        const uri = getMongoUri();
        mongoClient = new MongoClient(uri);
        await mongoClient.connect();
      }
      
      if (!databases.has(dbName)) {
        databases.set(dbName, mongoClient.db(dbName));
      }
      
      return databases.get(dbName)!;
    }

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