Firebase查询Or'ing whereEqualTo以获取可能值的列表 [英] Firebase Query Or'ing whereEqualTo for a list of possible values

查看:48
本文介绍了Firebase查询Or'ing whereEqualTo以获取可能值的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前见过此问题,对于较早版本的Firebase不再适用于Firebase Cloud Firestore.

I've seen this question before for an earlier version of Firebase that is no longer applicable for Firebase Cloud Firestore.

我想进行查询,以对照潜在值列表来检查文档字段的值,例如 whereEqualTo(someField,OR b OR c OR ... OR n).具体来说,我具有一个类型为Reference的单个字段的潜在值列表,并且我希望查询返回指定集合中其字段值等于列表中潜在值之一的所有文档.我也在使用 .limit(10),因为我正在分页数据.

I would like to make a query that checks a document's field's value against a list of potential values, something like whereEqualTo(someField, a OR b OR c OR ... OR n). Specifically, I have a list of potential values for a single field of type Reference and I want my query to return all documents in the specified collection whose value of the field is equal to one of the potential values in the list. I am also using .limit(10) because I am paginating data.

例如,如果我的列表包含引用[ref1,ref2,ref3],则该查询将类似于加入这三个查询:

So for example, if my list contained the References [ref1, ref2, ref3] then the query would be similar to joining these 3 queries:

查询query1 = mDatabase.collection("myCollection").whereEqualTo("refField",ref1);
查询query2 = mDatabase.collection("myCollection").whereEqualTo("refField",ref2);
查询query3 = mDatabase.collection("myCollection").whereEqualTo("refField",ref3);

Query query1 = mDatabase.collection("myCollection").whereEqualTo("refField", ref1);
Query query2 = mDatabase.collection("myCollection").whereEqualTo("refField", ref2);
Query query3 = mDatabase.collection("myCollection").whereEqualTo("refField", ref3);

文档的限制"部分下,它提到:

On the documentation, under the "Limitations" section, it mentions:

逻辑或查询.在这种情况下,您应该为每个OR条件创建一个单独的查询,并将查询结果合并到您的应用中.

所以也许最好的方法是合并查询结果,但是如何做到这一点,并且将结果限制为10个?

So maybe the best way would be to merge query results, but how could this be done, and with limiting to 10 results?

请注意,我正在使用Java进行Android开发.

Note that I am working with Java for Android Development.

推荐答案

我想进行查询,以对照潜在值列表检查文档字段的值.

I would like to make a query that checks a document's field's value against a list of potential values.

不幸的是,Firestore中没有查询可以帮助您实现这一目标.相反,您可以做的是获取所有 myCollection 集合下的文档,并根据可能的值列表检查所需的document字段值:

Unfortunately, there is no query in Firestore that can help you achieve this. What can you do instead is to get all the documents beneath your myCollection collection and check the value of document field you need against a list of potential values like this:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference myCollectionRef = rootRef.collection("myCollection");
myCollectionRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            List<String> yourList = new ArrayList<>();
            for (QueryDocumentSnapshot document : task.getResult()) {
                DocumentReference reference = document.getDocumentReference("refField");
                String stringReference = reference.getPath();
                if (yourList.contains(stringReference)) {
                    Log.d(TAG, "Reference found!");
                }
            }
        }
    }
});

我假设您的列表包含String类型的文档引用.如您所见,我们需要使用 getPath()方法来序列化从数据库中获取的 DocumentReference 对象,以获取描述文档在数据库中位置的字符串..要将路径字符串反序列化回 DocumentReference 对象,请使用

I assumed your list contains document references of type String. As you can see, we need to serialize a DocumentReference objects that we get from the database by using getPath() method to get a String that describes the document's location in the database. To deserialize that path String back into a DocumentReference object, use FirebaseFirestore.getInstance().document(path) method.

PS.如果您对分页感兴趣,则 如何通过将查询游标与limit()方法结合使用来对查询进行分页.我还建议您看看此 视频 更好的理解.

PS. If you are interested about pagination, this is how you can paginate queries by combining query cursors with the limit() method. I also recommend you take a look at this video for a better understanding.

这篇关于Firebase查询Or'ing whereEqualTo以获取可能值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆