query_collection
Retrieve Firestore documents from a collection using filters, ordering, and limits to find specific data in Firebase Emulator environments.
Instructions
Query a collection with filters
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collectionPath | Yes | Collection path | |
| filters | No | Array of filter objects: {field, operator, value} | |
| orderBy | No | Field to order by | |
| orderDirection | No | ||
| limit | No | Max documents to return (default: 20) |
Implementation Reference
- src/index.ts:272-291 (handler)The implementation of the `handleQueryCollection` function, which executes the actual Firestore query based on provided filters, ordering, and limits.
async function handleQueryCollection( collectionPath: string, filters?: QueryFilter[], orderBy?: string, orderDirection?: "asc" | "desc", limit = 20 ) { let query: FirebaseFirestore.Query = db.collection(collectionPath); if (filters) { for (const filter of filters) { query = query.where(filter.field, filter.operator, filter.value); } } if (orderBy) { query = query.orderBy(orderBy, orderDirection || "asc"); } query = query.limit(limit); const snapshot = await query.get(); return snapshot.docs.map(docToObject); } - src/index.ts:187-212 (schema)The tool definition and input schema registration for `query_collection` in `src/index.ts`.
name: "query_collection", description: "Query a collection with filters", inputSchema: { type: "object" as const, properties: { collectionPath: { type: "string", description: "Collection path" }, filters: { type: "array", description: "Array of filter objects: {field, operator, value}", items: { type: "object", properties: { field: { type: "string" }, operator: { type: "string", enum: ["==", "!=", "<", "<=", ">", ">=", "array-contains", "in", "array-contains-any"] }, value: {}, }, required: ["field", "operator", "value"], }, }, orderBy: { type: "string", description: "Field to order by" }, orderDirection: { type: "string", enum: ["asc", "desc"] }, limit: { type: "number", description: "Max documents to return (default: 20)" }, }, required: ["collectionPath"], }, }, - src/index.ts:407-415 (registration)The registration of the `query_collection` case within the `CallToolRequestSchema` handler in `src/index.ts`.
case "query_collection": result = await handleQueryCollection( args?.collectionPath as string, args?.filters as QueryFilter[], args?.orderBy as string, args?.orderDirection as "asc" | "desc", args?.limit as number ); break;