11
votes

I'm stuck with querying Firestore REST API. I can figure out how to filter by a regular key but I can't figure out how to query by nested object key.

Basically, in the browser client, I can do

firestore.collection("channels")
   .where("members.[memberID]", "==", true)
   .get()

However, when I try to do the same in the REST API, it said Invalid Argument. I was trying to do

{
 "structuredQuery": {
  "where": {
   "fieldFilter": {
    "field": {
     "fieldPath": "members.[memberID]"
    },
    "op": "EQUAL",
    "value": {
       booleanValue: "true"
    }
   }
  },
  "from": [
   {
    "collectionId": "channels"
   }
  ]
 }
}

but it gives me "Invalid Argument" error on fieldPath. Does anybody know how to query Firestore REST API based on nested object?

Thank you!

1
I have the same problem ... can't find a good answer to it. - Saviz
What I may have to do is convert it to a string using JSONModel and then convert it back. Which seems a bit ugly. - Saviz
Sorry for the dumb question but where do you pass this object with the structured query? I am passing it as request body and it does not work at all. Could someone share a whole example? Would be helpful. - Georgi Arnaudov
@GeorgiArnaudov you need to pass the whole structuredQuery object in the request body, what I have above is the request body - OmomSun

1 Answers

12
votes

I asked for Firebase support and they said special characters in fieldPath such as "-" in my case can be escaped using back tick or back slash. It is mentioned in the documentation. In my case, I'll need to do

members.`[memberID]`

I hope this is helpful for you guys.