简体   繁体   English

如何在 Firestore 中搜索数组并查找用户 ID

[英]How to search array and find user ID in Firestore

Firstly, I am sorry for my terrible english.首先,我为我糟糕的英语感到抱歉。

I am trying to learn and make a Firestore app.我正在尝试学习和制作 Firestore 应用程序。 In my app, I get the colors of users.在我的应用程序中,我获得了用户的颜色。 If other users chosen the same color, it matches the users.如果其他用户选择了相同的颜色,则匹配用户。 It is very simple app but I cant write the query and get userId.这是一个非常简单的应用程序,但我无法编写查询并获取用户 ID。

My database example:我的数据库示例:

在此处输入图片说明

cRef = fStore.collection("Users");
cRef.whereArrayContains("colors", "red").get();

I have read many articles but could not understand, how can I list the UserId's with this 'whereArrayContains()' method?我已经阅读了很多文章,但无法理解,如何使用“whereArrayContains()”方法列出 UserId? Thank you for your helpings.谢谢你的帮助。

You should indeed use the array-contains operator to filter based on array values.您确实应该使用array-contains运算符根据数组值进行过滤。

In your case you would do something along these lines:在您的情况下,您会按照以下方式做一些事情:

cRef = fStore.collection("Users");

cRef.whereArrayContains("colors", "red")
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        Log.d(TAG, document.getId() + " => " + document.getData());
                    }
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });

More details in the doc here and here . 此处此处的文档中的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM