我喜欢使用fi恢复云函数计算子集合中的文档数量。
我的数据库如下所示:组/{groupId}/成员/{memberId}
我喜欢计算每个组的成员数(memberId)。这意味着每个组可以有不同数量的成员,并且可以灵活地增加或减少。
会对你的想法感到高兴:-)。
我花了一段时间才使这个工作,所以我想把它分享给其他人使用:
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.countDocumentsChange = functions.firestore.document('library/{categoryId}/documents/{documentId}').onWrite((change, context) => {
const categoryId = context.params.categoryId;
const categoryRef = db.collection('library').doc(categoryId)
let FieldValue = require('firebase-admin').firestore.FieldValue;
if (!change.before.exists) {
// new document created : add one to count
categoryRef.update({numberOfDocs: FieldValue.increment(1)});
console.log("%s numberOfDocs incremented by 1", categoryId);
} else if (change.before.exists && change.after.exists) {
// updating existing document : Do nothing
} else if (!change.after.exists) {
// deleting document : subtract one from count
categoryRef.update({numberOfDocs: FieldValue.increment(-1)});
console.log("%s numberOfDocs decremented by 1", categoryId);
}
return 0;
});
我想到了两种可能的方法。
1、直接统计合集的文档
您将使用QuerySnapshot
的size
属性,例如
admin.firestore().collection('groups/{groupId}/members/{memberId}')
.get()
.then(querySnapshot => {
console.log(querySnapshot.size);
//....
return null;
});
这里的主要问题是成本,如果子集合包含大量文档:通过执行此查询,您将对子集合的每个文档收取一次读取费用。
2、另一种做法是每个子集合维护一些计数器
您将编写两个基于分布式计数器的云函数,如本Firebase留档项所示:https://firebase.google.com/docs/firestore/solutions/counters.我们在以下示例中使用3个分片。
首先,当将新文档添加到subCollec
子集合时,Cloud Function会增加计数器:
//....
const num_shards = 3;
//....
exports.incrementSubCollecCounter = functions
.firestore.document('groups/{groupId}/members/{memberId}')
.onCreate((snap, context) => {
const groupId = context.params.groupId;
const shard_id = Math.floor(Math.random() * num_shards).toString();
const shard_ref = admin
.firestore()
.collection('shards' + groupId)
.doc(shard_id);
if (!snap.data().counterIncremented) {
return admin.firestore().runTransaction(t => {
return t
.get(shard_ref)
.then(doc => {
if (!doc.exists) {
throw new Error(
'Shard doc #' +
shard_id +
' does not exist.'
);
} else {
const new_count = doc.data().count + 1;
return t.update(shard_ref, { count: new_count });
}
})
.then(() => {
return t.update(snap.ref, {
counterIncremented: true //This is important to have the Function idempotent, see https://cloud.google.com/functions/docs/bestpractices/tips#write_idempotent_functions
});
});
});
} else {
console.log('counterIncremented NOT NULL');
return null;
}
});
然后,当从subCollec
子集合中删除文档时,第二个Cloud Function将减少计数器:
exports.decrementSubCollecCounter = functions
.firestore.document('groups/{groupId}/members/{memberId}')
.onDelete((snap, context) => {
const groupId = context.params.groupId;
const shard_id = Math.floor(Math.random() * num_shards).toString();
const shard_ref = admin
.firestore()
.collection('shards' + groupId)
.doc(shard_id);
return admin.firestore().runTransaction(t => {
return t.get(shard_ref).then(doc => {
if (!doc.exists) {
throw new Error(
'Shard doc #' +
shard_id +
' does not exist.'
);
} else {
const new_count = doc.data().count - 1;
return t.update(shard_ref, { count: new_count });
}
});
});
});
在这里,与解决方案1相比,由于我们有3个分片,当您想知道subCollec
子集合中的文档数量时,您只需要读取3个文档。
查看留档以获取有关如何初始化分布式计数器的详细信息。您必须为每个groupId
集合初始化一次(即admin. fi恢复().集合('shards'groupId)
)