1

I have a data structure like

 /Split       // Collection
   {authid}   // Document
      /SentIvitation //Collection
          {auto-id}  //Documnet
            amount    //array
             0:10
             1:10
             2:10
             Phonenumbers//array
               0:987654321
               1:123456789
               2:234567890

I need to query all the elements in Phone number and I have to check whether these phone numbers is already exist in another collection. The next path of my data structure is

/deyaUsers   //Collection
  {authid}.   //Documnet
      Name: "abc"
      Email: "[email protected]"
      Phonenumber:987654321

Is it possible in firestore without any indexing?

2 Answers 2

3

As in the official documentation:

Although Cloud Firestore can store arrays, it does not support querying array members or updating single array elements.

If you only want to get the entire papers array you need to iterate over a Map like this:

Map<String, Object> map = documentSnapshot.getData();
for (Map.Entry<String, Object> entry : map.entrySet()) {
    if (entry.getKey().equals("Phonenumbers")) {
        Log.d("TAG", entry.getValue().toString());
    }
}

But note, even if Phonenumbers object is stored in the database as an array, entry.getValue() returns an ArrayList, not an array.

You need to use indexes only when you query your database using more then one property. But is not your case.

A better approach will be if you consider this alternative database structure, where each phone number is the key in a map and all values are true:

Phonenumbers: {
    "987654321": true,
    "123456789": true,
    "234567890": true
}

Edit 13 Aug 2018:

According to the updated documentation regarding array membership, now it is possible to filter data based on array values using whereArrayContains() method. A simple example would be:

CollectionReference citiesRef = db.collection("cities");
citiesRef.whereArrayContains("regions", "west_coast");

This query returns every city document where the regions field is an array that contains west_coast. If the array has multiple instances of the value you query on, the document is included in the results only once.

Sign up to request clarification or add additional context in comments.

6 Comments

There is an alternate path
In order to check if those phone numbers exist in another collection, first you need to get them. But this is not possible in Cloud Firestore. You cannot query array members.
can u say then how to store phonenumber array into firestore for the abouve requirement
phone numbers are in a long array. In my case 987654321 are values. can we use it as filename and we can fetch that .
Yes and this is the alternative structure, in which phone numbers will be keys in a Map.
|
0

You need to have query like:

As you are querying from collection to document and then collection from document.

Explanation:

with parent node as "Split" first fetch the "Split" collection then there can be multiple documents according to "authid" therefore you will get document according to the currentUser (User which has signed in, fetched from FirebaseAuth).

Once you got the document for particular User according to authId you will fetch "SendInvitations" for that particular user collection from the authId document

Code:

CurrentUser currentUser = FirebaseAuth.getCurrentUser();

firestore.collection("Split").document(currentUser.getUid()).collection("SendInvitation")
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                if (task.isSuccessful()) {
                                    //Handle Success
                                } else {
                                    //Handle Error
                                }
                            }
                        });

As per the structure this will be query.

Hope it helps.

5 Comments

I have edited my question Now how I need to write query
The query will still work with the update structure.
Can u explain it
Please check the answer
here i need only phone number field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.