Skip to main content
Glama
jonfreeland

MongoDB MCP Server

by jonfreeland

get_distinct_values

Retrieve unique values from a specific field in a MongoDB collection to analyze data distribution, identify categories, or perform quality checks.

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
NameRequiredDescriptionDefault
databaseNoDatabase name (optional if default database is configured)
collectionYesCollection name
fieldYesField name to get distinct values for
filterNoMongoDB query filter to apply before getting distinct values (optional)

Implementation Reference

  • The handler for the 'get_distinct_values' tool. Extracts parameters, connects to the MongoDB database, executes the distinct operation 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)
    Tool registration in the list_tools response, including the name, description, and input schema definition for 'get_distinct_values'.
    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'], }, },
  • 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'], }, },

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