首页 > 解决方案 > Cloud Functions 中使用的 Firestore where 子句中是否有“!=”运算符?

问题描述

我想从 GET 请求中排除一些文档,所以我编写了以下代码(注意行users = users.where(admin.firestore.FieldPath.documentId(), '!=', k);):

let users = admin_firestore.collection('users');
const likes = admin_firestore.collection('likes_of_users_posts');
likes.get().then(function(likes_docs) {
        const map_users_id_with_number_of_likes = [];
        likes_docs.forEach(like_doc => {
            if(!(like_doc.data().post_owner in map_users_id_with_number_of_likes)) {
                map_users_id_with_number_of_likes[like_doc.data().post_owner] = 0;
            }
            map_users_id_with_number_of_likes[like_doc.data().post_owner] += 1;
        });
        Object.keys(map_users_id_with_number_of_likes).forEach((k) => {
            users = users.where(admin.firestore.FieldPath.documentId(), '!=', k);
        });

它是我的 Google Cloud Functions 功能的一部分。

当我在 Android 应用程序中执行此 Cloud Function 函数时,我可以在日志中看到以下错误:

错误:参数“opStr”的值无效。可接受的值为:<、<=、==、>、>=、数组包含

但是我真的需要写一些相当于:“不同于 X 并且不同于 Y 和......”。所以.where(admin.firestore.FieldPath.documentId(), '!=', k)像我一样链接。

标签: firebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


不,没有!=用于 Firestore 查询的运算符。原因是数据库的索引方式,有关更多信息,请参阅此官方视频

有一些变通方法,例如,将一个查询与>运算符结合起来,将另一个与<一个结合起来。本文展示了如何组合两个不同的查询以模拟 OR 查询。

我不确定这在您的情况下是否可行,因为您提到“不同于 X 并且不同于 Y 并且......”。您可能应该混合使用本文中描述的技术来模拟 OR 查询并在前端删除一些记录。


推荐阅读