如何从 Firestore 查询中排除元素? [英] How to exclude an element from a Firestore query?

查看:24
本文介绍了如何从 Firestore 查询中排除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组用户,我想从数据库中查询所有用户并将它们显示在 RecyclerView 中,除了一个,我的.这是我的数据库架构:

I have a collection of users and I want to query all users from the database and display them in a RecyclerView except one, mine. This is my db schema:

users [collection]
  - uid [document]
     - uid: "fR5bih7SysccRu2Gu9990TeSSyg2"
     - username: "John"
     - age: 22
  - //other users

如何像这样查询数据库:

How to query the database like so:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query q = db.collection("users").whereNotEqualTo("uid", uid);

所以我需要将此查询对象传递给 FirestoreRecyclerOptions 对象,以便在 RecyclerView 中显示所有其他用户.

So I need this query object to be passed to a FirestoreRecyclerOptions object in order to display all the other users in RecyclerView.

这甚至可能吗?如果没有,我该如何解决这个问题?谢谢!

Is this even possible? If not, how can I solve this? Thanks!

options = new FirestoreRecyclerOptions.Builder<UserModel>()
        .setQuery(query, new SnapshotParser<UserModel>() {
            @NonNull
            @Override
            public UserModel parseSnapshot(@NonNull DocumentSnapshot snapshot) {
                UserModel userModel = documentSnapshot.toObject(UserModel.class);
                if (!userModel.getUid().equals(uid)) {
                    return userModel;
                } else {
                    return new UserModel();
                }
            }
        }).build();

推荐答案

在这个问题上挣扎了几天之后,我终于找到了答案.没有@Raj 的帮助,我无法解决这个问题.非常感谢@Raj 的耐心和指导.

After days and days of struggling with this issue, I finally found the answer. I could not solve this without the help of @Raj. Thank you so much @Raj for the patience and guidance.

首先,根据@Frank van Puffelen 在他的回答中提供的答案 post,我不再寻找可以帮助我将两个查询传递给单个适配器的解决方案.

First off all, according to the answer provided by @Frank van Puffelen in his answer from this post, I stopped searching for a solution that can help me pass two queries to a single adapter.

在这个问题中,我想要实现的只是查询数据库以获取除我之外的所有用户.所以因为我们不能将两个查询合并为一个实例,所以我发现我们可以合并两个查询的结果.所以我创建了两个查询:

In this question, all that I wanted to achieve was to query the database to get all the users except one, me. So because we cannot combine two queries into a single instance, I found that we can combine the result of both queries. So I have created two queries:

FirebaseFirestore db = FirebaseFirestore.getInstance();
Query firstQuery = db.collection("users").whereLessThan("uid", uid);
Query secondQuery = db.collection("users").whereGreaterThan("uid", uid);

我的用户对象有一个 UserModel (POJO) 类.我找到了不是一个,而是两种解决问题的方法.第一个是查询数据库以获取与第一个条件对应的所有用户对象并将它们添加到列表中.之后,再次查询数据库,获取符合第二个条件的其他用户对象,并将它们添加到相同列表中.现在我有一个列表,其中包含我需要的所有用户,但只有一个,即查询中具有该特定 id 的用户.这是未来访问者的代码:

I'm having a UserModel (POJO) class for my user object. I found not one, but two ways to solve the problem. The first one would be to query the database to get all user objects that correspond to the first criteria and add them to a list. After that, query the database again and get the other user objects that correspond to the second criteria and add them to the same list. Now I have a list that contains all the users that I need but one, the one with that particular id from the queries. This is the code for future visitors:

firstQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        List<UserModel> list = new ArrayList<>();
        if (task.isSuccessful()) {
            for (DocumentSnapshot document : task.getResult()) {
                UserModel userModel = document.toObject(UserModel.class);
                list.add(userModel);
            }

            secondQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (DocumentSnapshot document : task.getResult()) {
                            UserModel userModel = document.toObject(UserModel.class);
                            list.add(userModel);
                        }

                        //Use the list of users
                    }
                }
            });
        }
    }
});

第二种方法会更短,因为我使用 Tasks.whenAllSuccess() 像这样:

The second approach would be much shorter because I use Tasks.whenAllSuccess() like this:

Task firstTask = firstQuery.get();
Task secondTask = secondQuery.get();

Task combinedTask = Tasks.whenAllSuccess(firstTask, secondTask).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
        @Override
        public void onSuccess(List<Object> list) {
            //This is the list that I wanted
        }
});

这篇关于如何从 Firestore 查询中排除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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