21

I want to get data from my Firestore db ordered by documentId in descending order. When I call:

firestore.collection("users")
    .orderBy(FieldPath.documentId(), Query.Direction.DESCENDING)
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {...});

I am getting error:

FAILED_PRECONDITION: The query requires an index.

With a link to Firebase console for automatic index creation. Unfortunately the automatic creation doesn't seem to work in this case. When I click on Create Index I get a message:

__name__ only indexes are not supported

For manual index creation, the doc only says about indexing by field name (not documentId). Does anyone know how to get data from Firestore ordered by documentId in descending order?

I know I can re-order data on client side, but ordering by id is such a natural task that I must be missing something. Thank you. I use:

compile 'com.google.firebase:firebase-firestore:11.6.0'
Grimthorr
  • 6,856
  • 5
  • 41
  • 53
zelig74
  • 482
  • 1
  • 6
  • 15

3 Answers3

9

As stated in the Firestore document here, Cloud Firestore does not provide automatic ordering on the auto-generated docIDs.

Important: Unlike "push IDs" in the Firebase Realtime Database, Cloud Firestore auto-generated IDs do not provide any automatic ordering. If you want to be able to order your documents by creation date, you should store a timestamp as a field in the documents.

Hence you can achieve ordering by adding an extra field createdDate and just setting it with annotation @ServerTimestamp, declaring it before the class field in your user POJO like this.

@ServerTimestamp
Date createdDate;

Also you must ensure this field is null when the POJO is being written on Firestore, do not set any value to it before saving it on Firestore.

Dhara Bhavsar
  • 345
  • 4
  • 11
  • The reason that Firestore does not order auto-generated IDs led me to manualy call .orderBy(...). The problem is that ordering requires index whitch I don't know how to create on ID. – zelig74 Nov 23 '17 at 15:26
  • If I am understanding your query correctly then you still want to add the auto-generated Firestore ID as an index so as to order result based on it. There are **two** things here: 1.) Firebase team advises against using auto-generated ID at all for ordering the query results as they do not provide automatic ordering like RTDB push ID. 2.) You can only create document fields as a query index and ID is not part of the document. Also by default each doc field is indexed. Hence they are advising to add an extra field _createDate_ as above to order query result based on the document creation date. – Dhara Bhavsar Nov 27 '17 at 16:44
  • 4
    Why are you talking about creation date? Author of question didn't say about chronological sorting his documents. – Eugene Aug 19 '18 at 13:22
  • 1
    @EugeneGriaznov zelig74's desire to order by ID doesn't tell you he wants chronological sorting? Which order would you expect when sorting by ID? – Parziphal Nov 14 '18 at 21:35
  • 3
    @Parziphal, I expect chronologically random order when sort by ID. – Eugene Nov 16 '18 at 02:19
  • @Eugene - That makes no sense. – Adrian Bartholomew Dec 30 '18 at 06:35
1

The accepted answer here is not correct.

From here:

auto-generated IDs do not provide any automatic ordering.

Yes, because you don't know what the auto-generated ID will be. That does not mean the default sort order is not by id, in fact, another doc page says quite the opposite:

From here:

By default, a query retrieves all documents that satisfy the query in ascending order by document ID. You can specify the sort order for your data using orderBy(), and you can limit the number of documents retrieved using limit().

So yes, you can sort in descending order by document id:

.orderBy(firebase.firestore.FieldPath.documentId(), 'desc')

J

Jonathan
  • 3,893
  • 5
  • 46
  • 77
  • How can you do this with the new v9 modules? I'm getting an error saying `FieldPath.documentId()` isnt a function – sayandcode Jul 10 '22 at 08:18
-3

Use this to order in descending order based on id:

.collection("jobs")
.orderBy("", "desc")
Vinit Khandelwal
  • 517
  • 6
  • 10
  • 2
    Unfortunately this throws an error: "Error: Value for argument "fieldPath" is not a valid field path. Paths can't be empty..." – zelig74 Aug 09 '20 at 19:17