Skip to main content
Glama
jonfreeland

MongoDB MCP Server

by jonfreeland

find_by_ids

Retrieve multiple MongoDB documents by their IDs in a single request for efficient queries. Preserves ID order, supports filtering fields with projection, and handles string or ObjectId identifiers.

Instructions

Find multiple documents by their IDs in a single request.

Advantages:

  • More efficient than multiple single document lookups

  • Preserves ID order in results when possible

  • Can filter specific fields with projection

  • Handles both string and ObjectId identifiers

Example: use_mcp_tool with server_name: "mongodb", tool_name: "find_by_ids", arguments: { "collection": "products", "ids": ["5f8d0f3c", "5f8d0f3d", "5f8d0f3e"], "idField": "_id", "projection": { "name": 1, "price": 1 } }

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionYesCollection name
databaseNoDatabase name (optional if default database is configured)
idFieldNoField containing the IDs (default: "_id")
idsYesArray of document IDs to look up
projectionNoMongoDB projection to specify fields to return (optional)

Implementation Reference

  • Handler for 'find_by_ids' tool: validates input, constructs MongoDB find query with $in on idField, applies projection if provided, retrieves documents, returns JSON.
    case 'find_by_ids': { const { database, collection, ids, idField = '_id', projection } = request.params.arguments as { database?: string; collection: string; ids: (string | number)[]; idField?: string; projection?: object; }; const dbName = database || this.defaultDatabase; if (!dbName) { throw new McpError( ErrorCode.InvalidRequest, 'Database name is required when no default database is configured' ); } if (!Array.isArray(ids) || ids.length === 0) { throw new McpError( ErrorCode.InvalidRequest, 'The ids parameter must be a non-empty array' ); } const db = client.db(dbName); let query = db.collection(collection).find({ [idField]: { $in: ids } }); if (projection) { query = query.project(projection); } const results = await query.toArray(); return { content: [ { type: 'text', text: JSON.stringify(results, null, 2), }, ], }; }
  • src/index.ts:794-842 (registration)
    Registration of 'find_by_ids' tool in list_tools response, including detailed description and complete input schema definition.
    name: 'find_by_ids', description: `Find multiple documents by their IDs in a single request. Advantages: - More efficient than multiple single document lookups - Preserves ID order in results when possible - Can filter specific fields with projection - Handles both string and ObjectId identifiers Example: use_mcp_tool with server_name: "mongodb", tool_name: "find_by_ids", arguments: { "collection": "products", "ids": ["5f8d0f3c", "5f8d0f3d", "5f8d0f3e"], "idField": "_id", "projection": { "name": 1, "price": 1 } }`, inputSchema: { type: 'object', properties: { database: { type: 'string', description: 'Database name (optional if default database is configured)', }, collection: { type: 'string', description: 'Collection name', }, ids: { type: 'array', description: 'Array of document IDs to look up', items: { type: ['string', 'number'], }, }, idField: { type: 'string', description: 'Field containing the IDs (default: "_id")', }, projection: { type: 'object', description: 'MongoDB projection to specify fields to return (optional)', }, }, required: ['collection', 'ids'], }, },

Other Tools

Related Tools

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/jonfreeland/mongodb-mcp'

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