31

Here is the schema for a test database:

Enter image description here

I want to write a query that can get all the documents where contacts have id=1 in any of the array index.

I have checked array_contains operator for Firestore, but my array has a map which then have field id.

2
  • Did you figure out how to query for the id/name level attributes? I'm stuck with the same problem. Commented Sep 9, 2020 at 5:08
  • check here stackoverflow.com/a/76637467/2126077 Commented Jul 7, 2023 at 13:35

4 Answers 4

31

You currently can't build a query that looks for an object field within an array. The only things you can find in an array are the entire contents of an array element, not a part of an element.

With NoSQL type databases, the usual practice is to structure you data in a way that suits your queries. So, in your case, you're going to have to structure your data to let you find documents where an array item contains a certain string. So, you could create another array that contains just the strings you want to query for. You will have make sure to keep these arrays up to date.

You could also reconsider the use of an array here. Arrays are good for positional data, but unless these contacts need to be stored in a certain order, you might want to instead store an object whose keys are the id, and whose field data also contains the id and name.

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

3 Comments

Thanks @Doug Stevenson - exactly what I was looking for. To clarify - it's not possible to query based on the keys of a map, right? (i.e. Only way is to create a separate array of only these keys)
Or a separate object where the keys map to something like true/false.
@DougStevenson out of interest, considering you still cannot query based on the keys of a map, what is the benefit of converting this to one? Either way you need the additional array of ids kept in sync. Unless of course you can foresee needing constant time lookup.
9

As Doug said, you can't query it, but, if you could structure your data to something that looks like this

  1. Store your data as a map
  2. Use id as key and name as value
  3. Now you can write a query that can get all the documents where contacts have id=1
db.collection("test").where("contacts.1", ">=", "").get()

Comments

1

One solution is, as Doug said, to add an array with the data you need to query, and keep it up-to-date.

Another solution is to make contacts as a sub collection of the document, and_ then you could make queries of the contacts and get its parent.

Enter image description here

final Query contacts = db.collectionGroup("contacts").whereEqualTo("id", 1);
for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {
  System.out.println(document.getId());
  System.out.println(document.getString("parent_id"));
}

If you don't want to add the parent as another field, you can get it from the parent reference. Check this answer to Firestore - get the parent document of a subcollection.

snapshot.getRef().getParent().getParent().collection("people")

Collection group queries

Comments

1

If you have an array of maps or objects and want to get the docoments that contains this specific map/object like this,

array_of_maps.png

you can use:

queryForCategory(categName: string, categAlias: string): Observable[] {
    firestore.collection('<Collection name>', ref =>

        ref.where('categories',
          "array-contains", {
            "alias": categAlias,
            "title": categName
          }

1 Comment

No, I don't believe this is valid. You can't pass an object to array-contains in 2024.

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.