first post so any tips would be appreciated. I am trying to set an 'orderBy' in a StructuredQuery using the REST api to firestore. Its also helpful to note that my query does work if the orderBy field is not used.
My query looks like this:
var collection = "testing";
var Query = { //body of the http request
"structuredQuery": {
"from": {
"collectionId":collection,
"allDescendants":true
},
"orderBy": {
"field": {
"fieldPath" : "uploaded"
},
"direction" : "DESCENDING"
},
"limit": 1
},
"newTransaction": {}
}
var urlPOST = "https://firestore.googleapis.com/v1beta1/projects/"+id+"/databases/"+database+"/documents:runQuery?key="+key;
var optPOST = { //http header
'method' : 'post',
'contentType' : 'application/json',
'muteHttpExceptions': true,
'payload': JSON.stringify(Query)
};
var response = UrlFetchApp.fetch(urlPOST, optPOST);
from this request i get:
"error": {
"code": 400,
"message": "The query requires a COLLECTION_GROUP_DESC index for collection testing and field uploaded. You can create it here: https://console.firebase.google.com/project/<mydatabase>/database/firestore/indexes/single-field ",
"status": "FAILED_PRECONDITION"
}
Googles firestore API doco says that it should give me a link to create the required index, but the link returned here does not do that, instead it returns me to the overview page.
A little more detail about what I'm trying to do: I want to be able to get the last uploaded document from the database. i do this by having a field 'uploaded' which is simply a TimeStamp field (uploaded via a python script using the firestore_admin library calling firestore.SERVER_TIMESTAMP)
Let me know if any more information is required
Thanks in Advance
Making REST callsAll REST API endpoints exist under the base URL https://firestore.googleapis.com/v1/ . To create a path to a document with the ID LA in the collection cities under the project YOUR_PROJECT_ID you would use the following structure. To interact with this path, combine it with the base API URL.
You can specify the sort order for your data using orderBy() , and you can limit the number of documents retrieved using limit() . Note: An orderBy() clause also filters for existence of the given field. The result set will not include documents that do not contain the given field.
Is there any way to get the last created document in Firebase Firestore collection? Yes, there is! The simplest way to achieve this is to add a date property to each object in your collection, then simply query it according to this new property descending and call limit(1) function. That's it!
TL;DR - the allDescendants
field doesn't do anything useful right now and you shouldn't use it. It was put into the protocol in anticipation of a feature called "Collection Group Queries" which has not yet launched.
As you can see, each Firestore query has a parent
and a target collectionId
, in addition to other constraint clauses.
In your case the parent
is /projects/$YOUR_PROJECT/datbases/(default)/
, so basically the root of your DB. The collectionId
is the name of the collection that you're searching for documents, which you have specified as testing
.
When allDescendants
is false then it will only search for collections with that name that are a direct descendant of the parent. In this case that means it will search the collection /testing
.
If allDescendants
is set to true
the search woudl cover any collection named "testing" at any depth under the parent. So not only /testing
but also something like /widgets/foo/testing
However fulfilling this query requires what we call a "collection group" index, as indicated in the error message. Right now there is no way for you to create an index like that, which is why I said above that this parameter is basically useless.
As we've stated on some other forums, we do intend to release this type of query in the future in which case the allDescendants
parameter would be more useful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With