寫入時間匯總

您可以透過 Cloud Firestore 的查詢功能 尋找大型集合中的文件如要深入瞭解集合整體的屬性,您可以匯總集合的資料。

您可以在讀取時或寫入時匯總資料:

  • 讀取時間匯總功能會在要求時計算結果。Cloud Firestore 可在讀取時支援 count()sum()average() 匯總查詢。與寫入時間匯總相比,讀取時間匯總查詢更容易新增至應用程式。如要進一步瞭解匯總查詢,請參閱使用匯總查詢產生資料摘要

  • 寫入時間匯總會在應用程式每次執行相關寫入作業時計算結果。寫入時間匯總較常實作,但您可以考慮使用這類匯總而非讀取時間匯總,原因如下:

    • 您想監聽匯總結果來即時更新。count()sum()average() 匯總查詢不支援即時更新。
    • 您想將匯總結果儲存在用戶端快取中。count()sum()average() 匯總查詢不支援快取。
    • 您正在為每位使用者匯總數萬份文件的資料,並考量成本。文件數量較少,讀取時間匯總費用會較低。對於匯總作業中的大量文件,寫入時間匯總可能較少。

您可以使用用戶端交易或 Cloud Functions 實作寫入時間匯總。以下各節說明如何實作寫入時間匯總。

解決方案:透過用戶端交易進行寫入時間匯總

假設有一款當地推薦應用程式,可協助使用者找到美味的餐廳。以下查詢會擷取特定餐廳的所有評分:

網站

db.collection("restaurants")
  .doc("arinell-pizza")
  .collection("ratings")
  .get();

Swift

注意:這項產品不適用於 watchOS 和 App Clip 目標。
do {
  let snapshot = try await db.collection("restaurants")
    .document("arinell-pizza")
    .collection("ratings")
    .getDocuments()
  print(snapshot)
} catch {
  print(error)
}

Objective-C

注意:這項產品不適用於 watchOS 和 App Clip 目標。
FIRQuery *query = [[[self.db collectionWithPath:@"restaurants"]
    documentWithPath:@"arinell-pizza"] collectionWithPath:@"ratings"];
[query getDocumentsWithCompletion:^(FIRQuerySnapshot * _Nullable snapshot,
                                    NSError * _Nullable error) {
  // ...
}];

Kotlin+KTX

db.collection("restaurants")
    .document("arinell-pizza")
    .collection("ratings")
    .get()

Java

db.collection("restaurants")
        .document("arinell-pizza")
        .collection("ratings")
        .get();

我們不必擷取所有評分再計算匯總資訊,而是將這項資訊儲存在餐廳文件本身中:

網站

var arinellDoc = {
  name: 'Arinell Pizza',
  avgRating: 4.65,
  numRatings: 683
};

Swift

注意:這項產品不適用於 watchOS 和 App Clip 目標。
struct Restaurant {

  let name: String
  let avgRating: Float
  let numRatings: Int

}

let arinell = Restaurant(name: "Arinell Pizza", avgRating: 4.65, numRatings: 683)

Objective-C

注意:這項產品不適用於 watchOS 和 App Clip 目標。
@interface FIRRestaurant : NSObject

@property (nonatomic, readonly) NSString *name;
@property (nonatomic, readonly) float averageRating;
@property (nonatomic, readonly) NSInteger ratingCount;

- (instancetype)initWithName:(NSString *)name
               averageRating:(float)averageRating
                 ratingCount:(NSInteger)ratingCount;

@end

@implementation FIRRestaurant

- (instancetype)initWithName:(NSString *)name
               averageRating:(float)averageRating
                 ratingCount:(NSInteger)ratingCount {
  self = [super init];
  if (self != nil) {
    _name = name;
    _averageRating = averageRating;
    _ratingCount = ratingCount;
  }
  return self;
}

@end

Kotlin+KTX

data class Restaurant(
    // default values required for use with "toObject"
    internal var name: String = "",
    internal var avgRating: Double = 0.0,
    internal var numRatings: Int = 0,
)
val arinell = Restaurant("Arinell Pizza", 4.65, 683)

Java

public class Restaurant {
    String name;
    double avgRating;
    int numRatings;

    public Restaurant(String name, double avgRating, int numRatings) {
        this.name = name;
        this.avgRating = avgRating;
        this.numRatings = numRatings;
    }
}
Restaurant arinell = new Restaurant("Arinell Pizza", 4.65, 683);

為了使這些匯總作業保持一致,每次在子集合中加入新評分時,就必須更新這些匯總。實現一致性的一種方法是在單一交易中執行新增和更新:

網站

function addRating(restaurantRef, rating) {
    // Create a reference for a new rating, for use inside the transaction
    var ratingRef = restaurantRef.collection('ratings').doc();

    // In a transaction, add the new rating and update the aggregate totals
    return db.runTransaction((transaction) => {
        return transaction.get(restaurantRef).then((res) => {
            if (!res.exists) {
                throw "Document does not exist!";
            }

            // Compute new number of ratings
            var newNumRatings = res.data().numRatings + 1;

            // Compute new average rating
            var oldRatingTotal = res.data().avgRating * res.data().numRatings;
            var newAvgRating = (oldRatingTotal + rating) / newNumRatings;

            // Commit to Firestore
            transaction.update(restaurantRef, {
                numRatings: newNumRatings,
                avgRating: newAvgRating
            });
            transaction.set(ratingRef, { rating: rating });
        });
    });
}

Swift

注意:這項產品不適用於 watchOS 和 App Clip 目標。
func addRatingTransaction(restaurantRef: DocumentReference, rating: Float) async {
  let ratingRef: DocumentReference = restaurantRef.collection("ratings").document()

  do {
    let _ = try await db.runTransaction({ (transaction, errorPointer) -> Any? in
      do {
        let restaurantDocument = try transaction.getDocument(restaurantRef).data()
        guard var restaurantData = restaurantDocument else { return nil }

        // Compute new number of ratings
        let numRatings = restaurantData["numRatings"] as! Int
        let newNumRatings = numRatings + 1

        // Compute new average rating
        let avgRating = restaurantData["avgRating"] as! Float
        let oldRatingTotal = avgRating * Float(numRatings)
        let newAvgRating = (oldRatingTotal + rating) / Float(newNumRatings)

        // Set new restaurant info
        restaurantData["numRatings"] = newNumRatings
        restaurantData["avgRating"] = newAvgRating

        // Commit to Firestore
        transaction.setData(restaurantData, forDocument: restaurantRef)
        transaction.setData(["rating": rating], forDocument: ratingRef)
      } catch {
        // Error getting restaurant data
        // ...
      }

      return nil
    })
  } catch {
    // ...
  }
}

Objective-C

注意:這項產品不適用於 watchOS 和 App Clip 目標。
- (void)addRatingTransactionWithRestaurantReference:(FIRDocumentReference *)restaurant
                                             rating:(float)rating {
  FIRDocumentReference *ratingReference =
      [[restaurant collectionWithPath:@"ratings"] documentWithAutoID];

  [self.db runTransactionWithBlock:^id (FIRTransaction *transaction,
                                        NSError **errorPointer) {
    FIRDocumentSnapshot *restaurantSnapshot =
        [transaction getDocument:restaurant error:errorPointer];

    if (restaurantSnapshot == nil) {
      return nil;
    }

    NSMutableDictionary *restaurantData = [restaurantSnapshot.data mutableCopy];
    if (restaurantData == nil) {
      return nil;
    }

    // Compute new number of ratings
    NSInteger ratingCount = [restaurantData[@"numRatings"] integerValue];
    NSInteger newRatingCount = ratingCount + 1;

    // Compute new average rating
    float averageRating = [restaurantData[@"avgRating"] floatValue];
    float newAverageRating = (averageRating * ratingCount + rating) / newRatingCount;

    // Set new restaurant info

    restaurantData[@"numRatings"] = @(newRatingCount);
    restaurantData[@"avgRating"] = @(newAverageRating);

    // Commit to Firestore
    [transaction setData:restaurantData forDocument:restaurant];
    [transaction setData:@{@"rating": @(rating)} forDocument:ratingReference];
    return nil;
  } completion:^(id  _Nullable result, NSError * _Nullable error) {
    // ...
  }];
}

Kotlin+KTX

private fun addRating(restaurantRef: DocumentReference, rating: Float): Task<Void> {
    // Create reference for new rating, for use inside the transaction
    val ratingRef = restaurantRef.collection("ratings").document()

    // In a transaction, add the new rating and update the aggregate totals
    return db.runTransaction { transaction ->
        val restaurant = transaction.get(restaurantRef).toObject<Restaurant>()!!

        // Compute new number of ratings
        val newNumRatings = restaurant.numRatings + 1

        // Compute new average rating
        val oldRatingTotal = restaurant.avgRating * restaurant.numRatings
        val newAvgRating = (oldRatingTotal + rating) / newNumRatings

        // Set new restaurant info
        restaurant.numRatings = newNumRatings
        restaurant.avgRating = newAvgRating

        // Update restaurant
        transaction.set(restaurantRef, restaurant)

        // Update rating
        val data = hashMapOf<String, Any>(
            "rating" to rating,
        )
        transaction.set(ratingRef, data, SetOptions.merge())

        null
    }
}

Java

private Task<Void> addRating(final DocumentReference restaurantRef, final float rating) {
    // Create reference for new rating, for use inside the transaction
    final DocumentReference ratingRef = restaurantRef.collection("ratings").document();

    // In a transaction, add the new rating and update the aggregate totals
    return db.runTransaction(new Transaction.Function<Void>() {
        @Override
        public Void apply(@NonNull Transaction transaction) throws FirebaseFirestoreException {
            Restaurant restaurant = transaction.get(restaurantRef).toObject(Restaurant.class);

            // Compute new number of ratings
            int newNumRatings = restaurant.numRatings + 1;

            // Compute new average rating
            double oldRatingTotal = restaurant.avgRating * restaurant.numRatings;
            double newAvgRating = (oldRatingTotal + rating) / newNumRatings;

            // Set new restaurant info
            restaurant.numRatings = newNumRatings;
            restaurant.avgRating = newAvgRating;

            // Update restaurant
            transaction.set(restaurantRef, restaurant);

            // Update rating
            Map<String, Object> data = new HashMap<>();
            data.put("rating", rating);
            transaction.set(ratingRef, data, SetOptions.merge());

            return null;
        }
    });
}

使用交易可讓匯總資料與基礎集合保持一致。如要進一步瞭解 Cloud Firestore 中的交易,請參閱交易和批次寫入作業

限制

上述解決方案示範如何使用 Cloud Firestore 用戶端程式庫匯總資料,但請注意下列限制:

  • 安全性 - 用戶端交易需要授予用戶端更新資料庫匯總資料的權限。雖然編寫進階安全性規則可以降低這種做法的風險,但這種做法可能不適用於所有情況。
  • 離線支援:使用者裝置離線時,用戶端交易就會失敗,這表示您必須在應用程式中處理這個情況,並在適當時間重試。
  • 效能 - 如果您的交易包含多個讀取、寫入和更新作業,可能需要對 Cloud Firestore 後端提出多項要求。使用行動裝置時可能需要耗費大量時間
  • 寫入速率 - 這個解決方案可能不適用於經常更新的匯總,因為 Cloud Firestore 文件每秒只能更新一次。此外,如果交易讀取在交易外修改的文件,則會重試有限次數,然後失敗。請參閱分散式計數器,瞭解需要更頻繁更新的匯總解決方案。

解決方案:使用 Cloud Functions 進行寫入時間匯總

如果用戶端交易不適合您的應用程式,您可以使用 Cloud 函式,在每次餐廳新增評分時更新匯總資訊:

Node.js

exports.aggregateRatings = functions.firestore
    .document('restaurants/{restId}/ratings/{ratingId}')
    .onWrite(async (change, context) => {
      // Get value of the newly added rating
      const ratingVal = change.after.data().rating;

      // Get a reference to the restaurant
      const restRef = db.collection('restaurants').doc(context.params.restId);

      // Update aggregations in a transaction
      await db.runTransaction(async (transaction) => {
        const restDoc = await transaction.get(restRef);

        // Compute new number of ratings
        const newNumRatings = restDoc.data().numRatings + 1;

        // Compute new average rating
        const oldRatingTotal = restDoc.data().avgRating * restDoc.data().numRatings;
        const newAvgRating = (oldRatingTotal + ratingVal) / newNumRatings;

        // Update restaurant info
        transaction.update(restRef, {
          avgRating: newAvgRating,
          numRatings: newNumRatings
        });
      });
    });

這項解決方案會將工作從用戶端卸載至代管函式,這表示行動應用程式可以不必等待交易完成,就能新增評分。在 Cloud 函式中執行的程式碼不受安全性規則約束,也就是說,您不再需要將匯總資料的寫入權限授予用戶端。

限制

使用 Cloud 函式進行匯總可以避免用戶端交易發生一些問題,但會帶來不同的限制:

  • 費用:每次新增評分都會觸發 Cloud 函式叫用,進而增加您的費用。詳情請參閱 Cloud Functions 定價頁面
  • 延遲時間:將匯總工作卸載至 Cloud 函式後,應用程式必須等到 Cloud 函式執行完畢且收到新資料的通知後,才會看到更新的資料。視 Cloud 函式的速度而定,這可能需要比在本機執行交易的時間更長。
  • 寫入速率 - 這個解決方案可能不適用於經常更新的匯總,因為 Cloud Firestore 文件每秒只能更新一次。此外,如果交易讀取在交易外修改的文件,則會重試有限次數,然後失敗。請參閱分散式計數器,瞭解需要更頻繁更新的匯總解決方案。