Skip to main content
Glama
yaoxiaolinglong

MCP-MongoDB-MySQL-Server

mongodb_update

Update documents in a MongoDB collection using query filters and update operations to modify specific data entries.

Instructions

Update documents in a MongoDB collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionYesCollection name
filterYesMongoDB query filter
updateYesMongoDB update operations
manyNoUpdate multiple documents if true

Implementation Reference

  • The handler function that executes the MongoDB update operation. Ensures MongoDB connection, validates arguments, performs updateOne or updateMany based on the 'many' flag, and returns the result.
    private async handleMongoDBUpdate(args: any) {
      await this.ensureMongoConnection();
    
      if (!args.collection || !args.filter || !args.update) {
        throw new McpError(ErrorCode.InvalidParams, 'Collection name, filter, and update are required');
      }
    
      try {
        const collection = this.mongoDB!.collection(args.collection);
        let result;
        
        if (args.many) {
          result = await collection.updateMany(args.filter, args.update);
        } else {
          result = await collection.updateOne(args.filter, args.update);
        }
        
        return {
          content: [
            {
              type: 'text',
              text: `Successfully updated ${result.modifiedCount} documents`,
            },
          ],
        };
      } catch (error) {
        throw new McpError(
          ErrorCode.InternalError,
          `Failed to update documents: ${getErrorMessage(error)}`
        );
      }
    }
  • Input schema for the mongodb_update tool, defining required properties: collection, filter, update, and optional many boolean.
    inputSchema: {
      type: 'object',
      properties: {
        collection: {
          type: 'string',
          description: 'Collection name',
        },
        filter: {
          type: 'object',
          description: 'MongoDB query filter',
        },
        update: {
          type: 'object',
          description: 'MongoDB update operations',
        },
        many: {
          type: 'boolean',
          description: 'Update multiple documents if true',
          optional: true
        }
      },
      required: ['collection', 'filter', 'update'],
    },
  • src/index.ts:463-489 (registration)
    Registration of the mongodb_update tool in the list of tools returned by ListToolsRequest, including name, description, and input schema.
    {
      name: 'mongodb_update',
      description: 'Update documents in a MongoDB collection',
      inputSchema: {
        type: 'object',
        properties: {
          collection: {
            type: 'string',
            description: 'Collection name',
          },
          filter: {
            type: 'object',
            description: 'MongoDB query filter',
          },
          update: {
            type: 'object',
            description: 'MongoDB update operations',
          },
          many: {
            type: 'boolean',
            description: 'Update multiple documents if true',
            optional: true
          }
        },
        required: ['collection', 'filter', 'update'],
      },
    },
  • src/index.ts:560-561 (registration)
    Dispatch registration in the CallToolRequest handler switch statement, routing calls to the handleMongoDBUpdate method.
    case 'mongodb_update':
      return await this.handleMongoDBUpdate(request.params.arguments);

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/yaoxiaolinglong/mcp-mongodb-mysql-server'

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