get_distinct_values
Retrieve unique values from a specified field in a MongoDB collection to analyze data distribution, identify categories, check quality, or detect anomalies within filtered datasets.
Instructions
Get distinct values for a field in a collection.
Useful for:
Understanding data distribution
Finding unique categories
Data quality checks
Identifying outliers
Example: use_mcp_tool with server_name: "mongodb", tool_name: "get_distinct_values", arguments: { "collection": "users", "field": "role", "filter": { "active": true } }
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| database | No | Database name (optional if default database is configured) | |
| field | Yes | Field name to get distinct values for | |
| filter | No | MongoDB query filter to apply before getting distinct values (optional) |
Implementation Reference
- src/index.ts:1279-1304 (handler)Handler for the get_distinct_values tool. Extracts parameters, connects to the database, executes MongoDB's distinct() method on the specified field with optional filter, and returns the unique values as JSON.case 'get_distinct_values': { const { database, collection, field, filter } = request.params.arguments as { database?: string; collection: string; field: string; filter?: object; }; const dbName = database || this.defaultDatabase; if (!dbName) { throw new McpError( ErrorCode.InvalidRequest, 'Database name is required when no default database is configured' ); } const db = client.db(dbName); const values = await db.collection(collection).distinct(field, filter || {}); return { content: [ { type: 'text', text: JSON.stringify(values, null, 2), }, ], }; }
- src/index.ts:639-679 (registration)Registration of the get_distinct_values tool in the list_tools handler, including name, description, and input schema.name: 'get_distinct_values', description: `Get distinct values for a field in a collection. Useful for: - Understanding data distribution - Finding unique categories - Data quality checks - Identifying outliers Example: use_mcp_tool with server_name: "mongodb", tool_name: "get_distinct_values", arguments: { "collection": "users", "field": "role", "filter": { "active": true } }`, inputSchema: { type: 'object', properties: { database: { type: 'string', description: 'Database name (optional if default database is configured)', }, collection: { type: 'string', description: 'Collection name', }, field: { type: 'string', description: 'Field name to get distinct values for', }, filter: { type: 'object', description: 'MongoDB query filter to apply before getting distinct values (optional)', }, }, required: ['collection', 'field'], }, },
- src/index.ts:657-678 (schema)Input schema definition for the get_distinct_values tool, specifying parameters like database, collection, field, and optional filter.inputSchema: { type: 'object', properties: { database: { type: 'string', description: 'Database name (optional if default database is configured)', }, collection: { type: 'string', description: 'Collection name', }, field: { type: 'string', description: 'Field name to get distinct values for', }, filter: { type: 'object', description: 'MongoDB query filter to apply before getting distinct values (optional)', }, }, required: ['collection', 'field'], },