Skip to content

[Firestore] #3036

@solarpush

Description

@solarpush

Step 2: Describe your environment
Operating System version: Ubuntu 24.04
Firebase SDK version: firebase-admin@13.5.0
@google-cloud/firestore version: 7.11.0
Firebase Product: Firestore
Node.js version: v20.19.0
NPM version: 11.3.0

Step 3: Describe the problem
When using AggregateField.average() on a query that includes Filter.or(), the aggregate returns an incorrect value. It seems to only consider one of the matching documents instead of computing the average across all matched documents.

Steps to reproduce:
Create a collection with documents containing a numeric field participantsRatingAvg
Query using Filter.or() combined with other where clauses
Use AggregateField.average("participantsRatingAvg") on that query
The result is incorrect
Sample code:

import { Filter, AggregateField } from "firebase-admin/firestore";

// Query with Filter.or()
const query = db.collectionGroup("events")
  .where("participantsCount", ">", 0)
  .where("activityId", "==", "some-activity-id")
  .where("dateTime", ">=", startDate)
  .where("dateTime", "<=", endDate)
  .where(
    Filter.or(
      Filter.where("residenceId", "==", residenceId),
      Filter.where("executionResidenceId", "==", residenceId)
    )
  )
  .where("status", "in", ["accepted", "invoiced", "signed", "custom"]);

// First, verify the documents exist and their field values
const snapshot = await query.get();
console.log("Documents found:", snapshot.docs.map(d => ({
  rating: d.data().participantsRatingAvg,
  ratingType: typeof d.data().participantsRatingAvg,
  hasField: "participantsRatingAvg" in d.data(),
})));
// Output:
// [
//   { rating: 1, ratingType: 'number', hasField: true },
//   { rating: 3, ratingType: 'number', hasField: true }
// ]

// Now run the aggregate
const aggregateSnapshot = await query.aggregate({
  avgRating: AggregateField.average("participantsRatingAvg"),
}).get();

console.log("Average:", aggregateSnapshot.data().avgRating);
// Expected: 2 (average of 1 and 3)
// Actual: 3 (only considers one document)

Expected behavior:
AggregateField.average("participantsRatingAvg") should return 2 (the average of 1 and 3).

Actual behavior:
AggregateField.average("participantsRatingAvg") returns 3, ignoring one of the documents that matches the query.

Additional context:
The same query with .get() correctly returns both documents
Both documents have participantsRatingAvg as a number type (verified with typeof)
Both documents have the field present (verified with "field" in doc.data())
AggregateField.sum() and AggregateField.count() on the same query were not verified
The issue seems specific to queries using Filter.or() combined with aggregations

Workaround:
Fetch documents with .get() and compute the average manually:

const snapshot = await query.get();
const docs = snapshot.docs.map(d => d.data());
const validRatings = docs.filter(d => typeof d.participantsRatingAvg === "number" && d.participantsRatingAvg > 0);
const avgRating = validRatings.length > 0
  ? validRatings.reduce((sum, d) => sum + d.participantsRatingAvg, 0) / validRatings.length
  : 0;

Best regards

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions