Phi*_*orz 6 python google-cloud-firestore
是否可以使用 python 计算 Firestore 中的集合有多少文档?
我刚刚找到这个代码
functions.firestore.document('collection/{documentUid}')
.onWrite((change, context) => {
if (!change.before.exists) {
// New document Created : add one to count
db.doc(docRef).update({numberOfDocs: FieldValue.increment(1)});
} else if (change.before.exists && change.after.exists) {
// Updating existing document : Do nothing
} else if (!change.after.exists) {
// Deleting document : subtract one from count
db.doc(docRef).update({numberOfDocs: FieldValue.increment(-1)});
}
return;
});
Run Code Online (Sandbox Code Playgroud)
我怎样才能用 python 做到这一点?
Firestore 现在已将该方法添加到count()Python SDK 中的查询和集合中。
用法
目前文档并没有对此进行太多说明,但以下是我如何使用它。
您需要firebase-admin6.1.0 或更高版本。
from firebase_admin import firestore
# Do the credentials stuff if necessary
db = firestore.client()
# Count all docs in collection
my_collection = db.collection("foo")
count_query = my_collection.count()
query_result = count_query.get()
print("Nb docs in collection:", query_result[0][0].value)
# Count some docs via a query
any_query = db.collection("foo").where("myField", "==", 0)
count_query = simple_query.count()
query_result = count_query.get()
print("Nb docs in query:", query_result[0][0].value)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3101 次 |
| 最近记录: |