9

What would be the right way to query a field that is an array of maps.

Currently the structure is

Collection1
     Document1
         -papers:                <---- This is an array  
              (0): 
                 -Name:abc
                 -Id:123 
              (1): 
                 -Name:xyz
                 -Id:456

And this is my code

DocumentReference docRef = db.collection("Collection1").document("Document1");
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document != null && document.exists()) {
                       //?? how can I retrieve papers
                }
            }
        });

Basically do I retrieve it and cast it as an ArrayList> and then loop through it to create my final ArrayList ?

Or how does it work ?

0

2 Answers 2

8

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.


As per official documentation regarding arrays:

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 = document.getData();
for (Map.Entry<String, Object> entry : map.entrySet()) {
    if (entry.getKey().equals("papers")) {
        Log.d("TAG", entry.getValue().toString());
    }
}

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

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

2 Comments

You can't query array members meaning you can't do filteration based on member but offcourse you can get all the array members data. Otherwise why are they stored. I was able after I posted the question to get an array of hashmap and then iterates over it to create my array of model. I was hoping there is a shorter way though
Sorry if I made you confused. Yes, every document in Cloud Firestore is a Map so you need to iterate in order to get the array. If you have used a model class, you could call a getArray() method on the model class object. So please see my updated answer.
0

ArrayList<Map<String, Object>> arrayInTheDocument = (ArrayList<Map<String, Object>> ) documentSnapshot.getData().get("documentName.ArrayName");

Comments

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.