简体   繁体   English

在 Android (Java) 中,从包含当前用户的 email 地址的文档中获取所有评论

[英]In Android (Java) get all reviews from documents which contain the current user's email address

In my collection of eateries, each eatery has a unique random ID.在我收集的餐馆中,每个餐馆都有一个唯一的随机 ID。 Within each eatery document there may be a field named 'reviews'.在每个餐馆文件中可能有一个名为“评论”的字段。 Each review is a map and is stored under a user's email address.每条评论都是 map 并存储在用户的 email 地址下。 Is there any way I can query the database as a whole and get all the reviews for one user email address?有什么方法可以查询整个数据库并获取一个用户 email 地址的所有评论? I've tried several things, such as storing the user email as a field within the map and using dot notation to try and pull out the review, but to no avail.我尝试了几件事,例如将用户 email 存储为 map 中的一个字段,并使用点表示法尝试退出评论,但无济于事。 The best I've come up with so far:到目前为止我想出的最好的:

db.collection("eateries")
    .whereEqualTo("reviews" , userEmail)
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {

            Log.d(TAG, "restoreReviews onComplete: task.getResult(): " + task.getResult().size());

            if (task.isSuccessful()) {

                Log.d(TAG, "restoreReviews onComplete: task was successful");

                for (QueryDocumentSnapshot document : task.getResult()) {
                }

            } else {
                Log.d(TAG, "restoreReviews Error getting documents: ", task.getException());
            }
        }
    });

Just to reiterate, reviews contains maps of review objects (which are in turn also maps):重申一下,评论包含评论对象的地图(反过来也是地图):

在此处输入图像描述

A user could leave reviews for lots of eateries (in other documents) and I'd like to retrieve them all.用户可以留下许多餐馆的评论(在其他文档中),我想全部检索它们。 eateriesTest is just a dummy collection to show the types of documents I'm querying, without displaying users' actual email addresses. eateriesTest 只是一个虚拟集合,用于显示我正在查询的文档类型,而不显示用户的实际 email 地址。 Any pointers would be greatly appreciated.任何指针将不胜感激。

Because you're trying to query an array of reviews the whereEqualTo method doesnt do what youre looking for here.因为您正在尝试查询评论数组,所以 whereEqualTo 方法无法执行您在此处查找的内容。 If you replace it with whereArrayContains() method that should fix your problem.如果你用 whereArrayContains() 方法替换它应该可以解决你的问题。

The structure of your database does not allow for the kind of query you're trying to do.您的数据库结构不允许您尝试进行那种查询。 Firestore can't query for the existence of keys within a map field. Firestore 无法查询 map 字段中是否存在密钥。 It can only query on the values of map keys.它只能查询 map 键的值。

You will have to add the email addresses to the document in a way that can actually be queried.您必须以实际可以查询的方式将 email 地址添加到文档中。 For example, they could be stored as individual elements of an array type field, and filtered with an array-contains query.例如,它们可以存储为数组类型字段的单个元素,并使用包含数组的查询进行过滤。 It's common to duplicate information like this in order to satisfy your queries.为了满足您的查询,复制这样的信息是很常见的。

For example, if you have an array field "reviewers" with only the strings of each email, you could filter them like this:例如,如果您有一个数组字段“reviewers”,其中只有每个 email 的字符串,您可以像这样过滤它们:

.whereArrayContains("reviewers" , userEmail)

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

相关问题 如何获取 Android 用户的电子邮件地址? - How can you get an Android user's email address? Rally Rest Api:从缺陷中获取用户的电子邮件地址 - Rally Rest Api: Get User's Email Address from Defect Firebase / Android:获取当前用户电子邮件 - Firebase/Android: Get current user email Firestore 从根集合中获取所有文档(包含子集合) - Firestore get all documents (contain subcollections) from a root collection Android Java Google Places GET(拉出)评论JSONException没有评论的值 - Android Java Google Places GET (Pull) Reviews JSONException No value for reviews 如何获取当前登录用户的电子邮件地址? (Android Studio / Java / Firebase) - How do I get the email address of currently logged in user? (Android Studio / Java / Firebase) 从用户名Outlook Java获取电子邮件地址 - get email address from username outlook java 如何从 Android 设备(如 Google Pay 应用程序)获取用户名和电子邮件地址 - How to get user name and email address from android device like Google Pay app 在Java中获取用户当前的经度和经度 - Get user's current latitude and longitude in Java 设置默认电子邮件或其他地址以获取用户反馈 - Set default email or other address to get feedback from user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM