Firebase 如何计算云firestore中的文档数?
我在使用firestore DB实现计数器时遇到问题。 我有一个文档集合,其中文档有一个在某个点更新的字段 收藏 对象 字段:正确 对象 字段:false 对象 字段:正确 我需要计算值等于true的所有字段。同时从多个设备更新应用程序Firebase 如何计算云firestore中的文档数?,firebase,google-cloud-functions,google-cloud-firestore,Firebase,Google Cloud Functions,Google Cloud Firestore,我在使用firestore DB实现计数器时遇到问题。 我有一个文档集合,其中文档有一个在某个点更新的字段 收藏 对象 字段:正确 对象 字段:false 对象 字段:正确 我需要计算值等于true的所有字段。同时从多个设备更新应用程序 我怎样才能做到这一点?我是否需要使用分布式计数器方法o仅使用云函数就足够了云Firestore限制每文档每秒写入一次。我不建议让多个用户同时写入同一文档。你肯定应该使用这个方法。使用云函数。简单的例子: // database in the 'posts' co
我怎样才能做到这一点?我是否需要使用分布式计数器方法o仅使用云函数就足够了云Firestore限制每文档每秒写入一次。我不建议让多个用户同时写入同一文档。你肯定应该使用这个方法。使用云函数。简单的例子:
// database in the 'posts' collection
{
title: "My great post",
categories: {
"technology": true,
"opinion": false,
"cats": true
}
云函数脚本:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault()
});
const docRef = admin.firestore().collection('post')
.where('categories', '==', true)
.get()
.then((result) => {
res.send(result.size()); //return the count
}).catch(function(error){
console.log("got an error",error);
})
exports.countFunction = functions.https.onRequest(...);