我在Firebase的Cloud Firestore数据库中有一个集合,其子集合如下:
myCollection
doc1
statistics
doc1
doc2
doc3
doc2
statistics
doc1
doc2
doc3
statistics
doc1
doc2
doc3
doc4
doc4
statistics
等等
例如,根据查询,我可能从集合中提取doc1、doc2和doc4。现在,对于每一个,我需要查询它们各自的子集合以获取相关统计信息。
到目前为止,我在AngularJS应用程序中的解决方案是:
/**
* Gets aggregate views for queried docs.
* @param {![firebase.firestore.DocumentSnapshot]} docs - the queried documents
*/
$scope.getTotalViews = (docs) => {
let promises = [];
docs.forEach(doc => {
promises.push($scope.getTotalDocumentViews(doc.id));
});
$q.all(promises).then(totalViewsArray => {
// TODO: Sum the array to get aggregate views for queried documents
// Only outputs some of the time
console.log(totalViewsArray);
});
};
/**
* Gets all view statistics from a given document's subcollection.
*/
$scope.getTotalDocumentViews = (id) => {
let deferred = $q.defer();
firebase.firestore().collection("myCollection").doc(id).collection("statistics").where("type", "==", "view").get().then(snapshot => {
deferred.resolve(snapshot.size);
});
return deferred.promise;
};
我遇到的问题是,由于myCollection上的查询可能返回许多文档,因此循环遍历所有这些文档并查询其子集合似乎效率极低。不仅如此,尽管上述代码在某些时候确实成功,但在很多时候它会抛出错误:
Uncaught (in promise) Error: transaction closed
我还尝试在一个事务中执行多个子集合查询,但效果不太好,因为我不是只检索一个文档,而是从子集合中查询可能有数百个文档。
如何有效地查询一组多个文档的子集合?