0

I understand from this post that firestore queries don't currently provide for the following scenario. Given data like this:

somecollection/somedoc:
    foos: [ { name:"foo" email:"foo@bar.com" }, ... ]
somecollection/someotherdoc:
    foos: [ { name:"bar" email:"bar@foo.com" }, ... ]

I cannot write a query like:

FIRCollectionReference *ref = [defaultFirestore collectionWithPath:@"/somecollection"];
FIRQuery *query = [ref queryWhereField:@"foos.email" isEqualTo:@"foo@bar.com"];

The advice in the other answer is to create a map of the array fields I wish to query, using the values as keys. Something like:

somecollection/somedoc:
    foos: [ { name:"foo" email:"foo@bar.com" }, ... ]
    emails: { "foo@bar.com": true, ... }
somecollection/someotherdoc:
    foos: [ { name:"bar" email:"bar@foo.com" }, ... ]
    emails: { "bar@foo.com": true, ... }

Now my query would be:

FIRCollectionReference *ref = [defaultFirestore collectionWithPath:@"/somecollection"];
FIRQuery *query = [ref queryWhereField:@"emails.foo@bar.com" isEqualTo:@(YES)];

But this query returns no documents, owing to the fact I think, that an email address isn't good syntax for a map's key. Among other potential noise, it contains a dot . which I fear looks to the FIRQuery as a dereferencing operator.

Can someone explain how to query on emails in an array (or any other object that seems ill-suited as a key in map)?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user1272965
  • 2,814
  • 8
  • 29
  • 49

1 Answers1

0

Looking through the headers, I found there's a variety of the query called queryWhereFieldPath:isEqualTo: which takes an FIRFieldPath, and a field path seems to be tolerant what would otherwise be considered delimiters in the field names. It's easily formed and used like this:

FIRFieldPath *path = [[FIRFieldPath alloc] initWithFields:@[@"emails", @"foo@bar.com"]];
FIRQuery *query = [ref queryWhereFieldPath:path isEqualTo:@(YES)];

So, yay. Strange, though, that this grown-up-seeming database has such a toy-like query language.

user1272965
  • 2,814
  • 8
  • 29
  • 49