0

I'm writing a cloud function which updates a sibling collection onUpdate. I have the follwong:

await db.collection(
          `users/${userId}/rosters`).where(
          "athletes.keys", "array-contains", bid).get();

where "athletes" is a map attribute in each document in rosters. Does this logic not work? Is there some other way to do it? Thanks!!

Jonah Kornberg
  • 153
  • 1
  • 8

2 Answers2

2

Firestore queries support dot notation interpretation, allowing target paths such as athletes.keys to be filtered. This does require an index to be generated if one is not already and the path cannot be dynamic. For example: "athletes.users." + user.id is not valid.

Some answers for reference

DIGI Byte
  • 4,225
  • 1
  • 12
  • 20
  • The second part of [this answer](https://stackoverflow.com/a/66838632/3068190) may also be useful for searching bids in a document, just replace "members" with "bids". – samthecodingman Apr 29 '21 at 06:12
0

Thank you for the answers, I realize now I'm being silly and can use the map trick. This will return all rosters which contain a matching key in athletes.

 db.collection(
          `users/${userId}/rosters`).where(
          `athletes.${bid}`, ">=", "").get();

Sam's solution would also work by adding an "exists" key.

Jonah Kornberg
  • 153
  • 1
  • 8