1

I like to receive all documents where key is starting with "abs" for example.

The documents in the collection users have the following keys:
'abs34kl'
'abs24io'
'abi98io'

The query should return the first and the second document.

My idea was to do it with orderBy:

var query = await Firestore.instance.collection('users').orderBy(...)
  .startAt(_controller.text)
  .endAt(_controller.text+"\uf8ff").limit(10);

How can I orderBy key? Or is there a better way to do that?

Neli
  • 532
  • 1
  • 3
  • 15
  • I'm not clear on what this query should do. Could you edit the question to show some sample data of documents that you intend to match with this query? – Doug Stevenson Sep 09 '19 at 19:28
  • 1
    See [this answer](https://stackoverflow.com/a/52627798/10122791) – Augustin R Sep 09 '19 at 19:40
  • OK, you're saying you want to query a collection for documents whose ID starts with some string. This will be much easier if you put the document ID into a field in the document, the sort and query using that field. – Doug Stevenson Sep 09 '19 at 19:40
  • @DougStevenson, yes would be a solution. But is that really necessary to do it like that? – Neli Sep 09 '19 at 19:48
  • @Augustin-R the answer at the link you provided would mean that I need to add the key to the document itself to orderBy, right? – Neli Sep 09 '19 at 19:48
  • 1
    I don't know if Cloud Firestore indexes document IDs like it does for normal document fields, so it might not be possible unless you put the data in a field. In general, document IDs aren't used to contain data that you intend to query on. Usually they are just used as unique strings. – Doug Stevenson Sep 09 '19 at 19:54
  • ok, thanks a lot guys! – Neli Sep 09 '19 at 19:57

0 Answers0