mongodb_find
Query and retrieve documents from a MongoDB collection using a customizable filter, sorting, and pagination. Ideal for extracting specific data from MongoDB databases via the MCP server.
Instructions
Find documents in a MongoDB collection
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| filter | No | MongoDB query filter | |
| limit | No | Maximum number of documents to return | |
| skip | No | Number of documents to skip | |
| sort | No | Sort criteria |
Input Schema (JSON Schema)
{
"properties": {
"collection": {
"description": "Collection name",
"type": "string"
},
"filter": {
"description": "MongoDB query filter",
"optional": true,
"type": "object"
},
"limit": {
"description": "Maximum number of documents to return",
"optional": true,
"type": "number"
},
"skip": {
"description": "Number of documents to skip",
"optional": true,
"type": "number"
},
"sort": {
"description": "Sort criteria",
"optional": true,
"type": "object"
}
},
"required": [
"collection"
],
"type": "object"
}
Implementation Reference
- src/index.ts:977-1008 (handler)The handler function that implements the core logic for the 'mongodb_find' tool. It ensures MongoDB connection, validates collection name, constructs query filter and options (limit, skip, sort), executes find query, and returns JSON-formatted documents.private async handleMongoDBFind(args: any) { await this.ensureMongoConnection(); if (!args.collection) { throw new McpError(ErrorCode.InvalidParams, 'Collection name is required'); } try { const collection = this.mongoDB!.collection(args.collection); const filter = args.filter || {}; const options: any = {}; if (args.limit) options.limit = args.limit; if (args.skip) options.skip = args.skip; if (args.sort) options.sort = args.sort; const documents = await collection.find(filter, options).toArray(); return { content: [ { type: 'text', text: JSON.stringify(documents, null, 2), }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to find documents: ${getErrorMessage(error)}` ); } }
- src/index.ts:411-440 (schema)Input schema definition for the 'mongodb_find' tool, specifying parameters like collection (required), filter, limit, skip, and sort.inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name', }, filter: { type: 'object', description: 'MongoDB query filter', optional: true }, limit: { type: 'number', description: 'Maximum number of documents to return', optional: true }, skip: { type: 'number', description: 'Number of documents to skip', optional: true }, sort: { type: 'object', description: 'Sort criteria', optional: true } }, required: ['collection'], },
- src/index.ts:408-441 (registration)Tool registration in the ListTools response, including name, description, and input schema for 'mongodb_find'.{ name: 'mongodb_find', description: 'Find documents in a MongoDB collection', inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name', }, filter: { type: 'object', description: 'MongoDB query filter', optional: true }, limit: { type: 'number', description: 'Maximum number of documents to return', optional: true }, skip: { type: 'number', description: 'Number of documents to skip', optional: true }, sort: { type: 'object', description: 'Sort criteria', optional: true } }, required: ['collection'], }, },
- src/index.ts:556-557 (registration)Dispatcher case in the CallToolRequestHandler switch statement that routes 'mongodb_find' calls to the handleMongoDBFind method.case 'mongodb_find': return await this.handleMongoDBFind(request.params.arguments);