Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular firestore array_contains on objects

I have the following document:

{
    "ID" : "01",
    "Name" : "A1",
    "players" : [ {
        "active" : false,
        "name" : "dissident"
      }, {
        "active" : false,
        "name" : "ink"
      }
    ]
}

Wrote it down in JSON so it's easier to understand. Now what I want to do with firestore is query on the players[*].name. I have seen the new option to use "array_contains".

But I have seen all examples use an array of string instead of an array of objects. Is it possible to query over the players[*].name?

This is what I tried:

this.afs.collection('Locations', ref => ref.where('players.name', 'array-contains', _name))

Based on @camden_kid reaction I build:

this.locationsCollection = this.afs.collection('Locations', ref => ref.where('players', 'array-contains', {active : false, name : _name}) );
if(this.locationsCollection === undefined){
    this.locationsCollection = this.afs.collection('Locations', ref => ref.where('players', 'array-contains', {active : true, name : _name}) );
}

But this is depended on the active value to, this will not work on every kind of situation.

like image 871
Swoox Avatar asked Aug 28 '18 08:08

Swoox


1 Answers

You can try testing against the whole object, e.g.

this.afs.collection('Locations', ref => ref.where('players', 'array-contains', {active : false, name : _name}))
like image 109
camden_kid Avatar answered Nov 03 '22 06:11

camden_kid