Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use array-contains for array containing map [duplicate]

In Cloud Firestore I am needing to find matches in an array that contains maps using the array-contains query.

I can very easily match direct strings, numbers etc however it always fails when the values of the array is a map. The documentation doesn't allude to is this is possible or not.

Here is an example of some data

{
 data:
    [
        item_1: {...},
        item_2: {...}
    ]
}

and here is an example of the query

db.collection("list")
  .where("data", "array-contains", "item_1")
  .get()
  .then(collectionRef => {
      //do something with collection
  })

I am expecting the array-contains to match the top value (item_x) of the map, at the moment it doesn't match this. I assume this is because it wants to match the entire content of the map as the "value" of the array item.

Any help would be appreciated. I might have to re-think my data structure if this is not possible. For context I am using this in a cloud function to update items (they are a reference to a doc) in an array when their original doc is updated.

like image 364
Charklewis Avatar asked Apr 06 '19 22:04

Charklewis


1 Answers

array-contains queries only match the entire item in an array. If that item is an object, the query must provide the entire, exact object to match for equality in the array.

What you're doing right now isn't supported. You will have to provide some other field in the document to search against, possibly duplicating data in that document, which is common for NoSQL type databases.

like image 96
Doug Stevenson Avatar answered Nov 08 '22 08:11

Doug Stevenson