0

I'm trying to query a collection for an exact combination of values in an object in a document.

The 'array-contains' operator returns all the documents those properties appear across the collection when I'm looking to know if the exact combination exist or not in a document.

EXAMPLE: In a 'styles' collection, each document has an array that includes a unique color combination of styles. I want to query to see if the exact combination of colors already exists in the collection in one of the documents.

Collection of 'styles':

   {
        "style1" : [ { "color" : [ "blue", "red"] } ],
        "style2" : [ { "color" : [ "blue", "red", "green" ] } ],
        "style3" : [ { "color" : [ "blue", "red", "yellow", "grey" ] } ],
        "style4" : [ { "color" : [ "red", "green" , "yellow" ] } ] 
   }
    
    
        let checkStyleExist = this.afs.collection("styles", ref => ref
        .where("color", "array-contains", ["blue", "red"]))
        .ref.get()
        
        checkStyleExist.docs.map(res => console.log(res))

RESULT: I get a list of documents where blue and red appear. How do I query a collection for a document with the EXACT values in an object? The ["blue", "red"] does not work to query for an exact match.

Kato
  • 40,352
  • 6
  • 119
  • 149
  • 1
    Does this answer your question? [Firestore search array contains for multiple values](https://stackoverflow.com/questions/54987399/firestore-search-array-contains-for-multiple-values) Firestore doesn't currently support any query like `array-contains-all`. You can try converting the array to a map as mentioned in the linked answer. Here's another [example](https://stackoverflow.com/questions/68165288/how-to-query-the-document-which-contains-the-given-values-in-firestore-document/68165655) – Dharmaraj Jul 02 '21 at 18:25
  • Indices won't work for what I'm trying to accomplish. I already tried it and Firebase generates an index in its console. I also tried .where(`color.${colorCode.uid}`, '==', true) but Firebase threw an error : "Try catch error triggered FirebaseError: Function Query.where() called with invalid data. Invalid field path (participants2.[object Object]). Paths must not contain '~', '*', '/', '[', or ']'" I believe the where query only takes a string as a first parameter and won't a allow string interpolation. – bad legend Jul 04 '21 at 03:56

0 Answers0