Skip to main content
Glama
jonfreeland

MongoDB MCP Server

by jonfreeland

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

Implementation Reference

  • 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'], }, },
  • 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'], },

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