query_collection
Query collections using URL-style, regex, or MongoDB JSON queries. Supports filtering, pagination, sorting, and bulk operations like update or delete.
Instructions
Query data from a collection. Supports URL-style, regex, and MongoDB-style JSON queries with comparison operators. Can also query system collections like '_hooks' which contains deployment metadata including available API endpoints. Using delete, update or replace is very powerful but also dangerous, so use with caution.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name. Use '_hooks' to query deployment metadata and discover available API endpoints. | |
| query | No | Query expression. Supports multiple formats: URL-style ('name=Polly&type=Parrot'), regex ('name=/^po/'), or MongoDB-style JSON ('{"name": "Polly", "age": {"$gt": 5}}' for complex queries with operators like $gt, $lt, $gte, $lte, $ne, $in, $nin, $exists, $regex). To get the latest deployment info with API endpoints, use: collection='_hooks', limit=1, reverse=true (check the routehooks property for the available API endpoints) | |
| count | No | Count query results | |
| delete | No | Delete all items from query result | |
| update | No | Patch all items from query result with JSON string '{...}' | |
| replace | No | Replace all items from query result with JSON string '{...}' | |
| useindex | No | Use an indexed field to scan data in query | |
| start | No | Start value for index scan | |
| end | No | End value for index scan | |
| limit | No | Limit query result. Use limit=1 and reverse to get latest deployment from _hooks collection | |
| fields | No | Comma separated list of fields to include | |
| sort | No | Comma separated list of fields to sort by. Use '_id' to sort by creation time | |
| offset | No | Skip items before returning data in query result | |
| enqueue | No | Add query result to queue topic | |
| reverse | No | Scan index in reverse order. Use with sort='_id' to get newest records first | |
| csv | No | Output data in CSV format | |
| jsonl | No | Output data in JSONL format |
Implementation Reference
- src/index.ts:51-69 (schema)Zod schema definition for query_collection tool inputs. Defines all parameters: collection (required), query, count, delete, update, replace, useindex, start, end, limit, fields, sort, offset, enqueue, reverse, csv, jsonl.
const queryCollectionSchema = z.object({ collection: z.string(), query: z.string().optional(), count: z.boolean().optional(), delete: z.boolean().optional(), update: z.string().optional(), replace: z.string().optional(), useindex: z.string().optional(), start: z.string().optional(), end: z.string().optional(), limit: z.number().optional(), fields: z.string().optional(), sort: z.string().optional(), offset: z.number().optional(), enqueue: z.string().optional(), reverse: z.boolean().optional(), csv: z.boolean().optional(), jsonl: z.boolean().optional() }); - src/index.ts:208-235 (registration)Tool registration entry for query_collection. Defines the tool name, description, and inputSchema for the ListTools response.
{ name: "query_collection", description: "Query data from a collection. Supports URL-style, regex, and MongoDB-style JSON queries with comparison operators. Can also query system collections like '_hooks' which contains deployment metadata including available API endpoints. Using delete, update or replace is very powerful but also dangerous, so use with caution.", schema: queryCollectionSchema, inputSchema: { type: "object", properties: { collection: { type: "string", description: "Collection name. Use '_hooks' to query deployment metadata and discover available API endpoints." }, query: { type: "string", description: "Query expression. Supports multiple formats: URL-style ('name=Polly&type=Parrot'), regex ('name=/^po/'), or MongoDB-style JSON ('{\"name\": \"Polly\", \"age\": {\"$gt\": 5}}' for complex queries with operators like $gt, $lt, $gte, $lte, $ne, $in, $nin, $exists, $regex). To get the latest deployment info with API endpoints, use: collection='_hooks', limit=1, reverse=true (check the routehooks property for the available API endpoints)" }, count: { type: "boolean", description: "Count query results" }, delete: { type: "boolean", description: "Delete all items from query result" }, update: { type: "string", description: "Patch all items from query result with JSON string '{...}'" }, replace: { type: "string", description: "Replace all items from query result with JSON string '{...}'" }, useindex: { type: "string", description: "Use an indexed field to scan data in query" }, start: { type: "string", description: "Start value for index scan" }, end: { type: "string", description: "End value for index scan" }, limit: { type: "number", description: "Limit query result. Use limit=1 and reverse to get latest deployment from _hooks collection" }, fields: { type: "string", description: "Comma separated list of fields to include" }, sort: { type: "string", description: "Comma separated list of fields to sort by. Use '_id' to sort by creation time" }, offset: { type: "number", description: "Skip items before returning data in query result" }, enqueue: { type: "string", description: "Add query result to queue topic" }, reverse: { type: "boolean", description: "Scan index in reverse order. Use with sort='_id' to get newest records first" }, csv: { type: "boolean", description: "Output data in CSV format" }, jsonl: { type: "boolean", description: "Output data in JSONL format" } }, required: ["collection"] } }, - src/index.ts:603-675 (handler)Handler implementation for query_collection tool. Parses arguments, builds CLI arguments for the 'coho query' command, executes it via executeCohoCommand(), and returns the result as formatted JSON (or raw CSV/JSONL).
case "query_collection": { const { collection, query = "", count = false, delete: shouldDelete = false, update, replace, useindex, start, end, limit = count ? undefined : 100, fields, sort, offset, enqueue, reverse = false, csv = false, jsonl = false } = args as QueryCollectionArgs; console.error(`Querying collection: ${collection}`); const queryArgs = [ 'query', '--collection', collection, '--project', config.projectId, '--space', config.space ]; if (query) queryArgs.push('--query', query); if (count) queryArgs.push('--count'); if (shouldDelete) queryArgs.push('--delete'); if (update) queryArgs.push('--update', update); if (replace) queryArgs.push('--replace', replace); if (useindex) queryArgs.push('--useindex', useindex); if (start) queryArgs.push('--start', start); if (end) queryArgs.push('--end', end); if (limit) queryArgs.push('--limit', limit.toString()); if (fields) queryArgs.push('--fields', fields); if (sort) queryArgs.push('--sort', sort); if (offset) queryArgs.push('--offset', offset.toString()); if (enqueue) queryArgs.push('--enqueue', enqueue); if (reverse) queryArgs.push('--reverse'); if (csv) queryArgs.push('--csv'); if (jsonl) queryArgs.push('--jsonl'); const result = await executeCohoCommand(queryArgs); // If the output is CSV or JSONL format, return as is if (csv || jsonl) { return { content: [ { type: "text", text: result } ], isError: false }; } // Otherwise parse and format as JSON return { content: [ { type: "text", text: JSON.stringify(JSON.parse(result), null, 2) } ], isError: false }; }