8

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.

2
  • 1
    Have you tried testing against the whole object? E.g. this.afs.collection('Locations', ref => ref.where('players', 'array-contains', {active : false, name : _name})) Commented Aug 28, 2018 at 9:04
  • You can add this as an answer this is working :) Commented Aug 28, 2018 at 9:06

2 Answers 2

9

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

this.afs.collection('Locations', ref => ref.where('players', 'array-contains', {active : false, name : _name}))
Sign up to request clarification or add additional context in comments.

5 Comments

Like I explained in my question this is a good workaround but it will require also to check if active is true. If there are more object items this will not work.
@Swoox That is true and it is only possible to do one 'array-contains' test otherwise it throws an error. Let me have a think.
@Swoox Would spitting your data into two arrays of names help in any way? E.g. players_active, players_inactive.
This will make it quite complex and every number have to match eachother.
In the end I followed you solution I added location to the players and moved them to a different collection.
0

You can't use this on objects in the way where you are looking for one parameter. Firestore doesnʻt support this (annoyingly). I looked at several ways of doing this. John Delaney's Firestore Modeling course looks to address these issues and more.

A better way would be to have two arrays one containing just the id/guids as strings and use array-contains on that.

If you still need the objects just create/have the second array with the objects and when you add objects to one, you add the string to another.

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.