Only structured queries are supported FIRESTORE API REST

Viewed 113

I am trying to run a query with the rest api but I can´t get it to work, I am sending this body in json:

"structuredQuery": {
     "where": {
        "fieldFilter": {
            "field": {
                "fieldPath": "total"
            },
            "op": "EQUAL",
            "value": {
                "integerValue": "10",
            }
         }
     }, 
     "from": [{
        "collectionId": "Total"
     }]              
}

I am just testing if querys work, I have a collection called Total with documents, and these documents have a field called total that is an integer value. I am using a POST request with the following URL:

"https://firestore.googleapis.com/v1/projects/MY_PROJECT_NAME/databases/(default)/documents:runQuery"

and I am getting this error:

[{
  "error": {
    "code": 400,
    "message": "only structured queries are supported",
    "status": "INVALID_ARGUMENT"
  }
}]
1 Answers

The CollectionSelector in your structured query is missing the allDescendants field.

If you check this documentation you can see that this field is a flag which can either be true or false but not null, so you have to set this in the query otherwise it will not work.

Also you need to add the select clause to add all fields you want to get as a result of the query or keep it empty to return all fields.

Finally in the said documentation you can check that there is a proper order that should be respected, in which the from must be declared before where. So if you change your structured query to the following:

"structuredQuery": {
    "from": [{
        "collectionId": "Total"
        "allDescendants": false
     }],
     "select": { "fields": [] }, 
     "where": {
        "fieldFilter": {
            "field": {
                "fieldPath": "total"
            },
            "op": "EQUAL",
            "value": {
                "integerValue": "10",
            }
         }
     }       
}

It will work as expected.

Related