简体   繁体   中英

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. 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.

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? Thank you for your helpings.

You should indeed use the array-contains operator to filter based on array values.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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