How to sort by a field in a sub-collection in firebase cloud firestore in flutter - flutter

I am trying to query a cloud firestore database and i need it to return all the documents in the chats collection sorted by the timestamp field which is a field that all the documents in the messages sub-collection have.
i tried writing a query like this.
FirebaseFirestore.instance.collection("chats").orderBy("messages.timestamp", descending: true)].get(),
but it does not return any documents when actually there are some documents there.

Firestore can only order or filter on data in the documents that it returns. There is no ability to order or filter on data outside of those documents.
So if we want to order chats in the timestamp of the last message in that chat (a common use-case), you'll have to include a lastMessageTimestamp field in the chat document itself, and update that whenever a message is written in its messages subcollection. With that lastMessageTimestamp field in place in each chats document, you can then order and filter on that.

Create a new collection called messages and store all messages for every user there (with a user id field). Reference the message uid's via an array in each chat. This way you can easily query for the messages associated with a chat session then sort them.

Related

How to query latest documents first?

I am using Flutter & Cloud Firestore. I have a collection. There are many documents in that collection. My question is how to fetch those documents on the time basis. Like I want to firstly show those documents data that I had added latest and the oldest documents data at the last. How to do that?
Considering you have a timestamp field (named addedAt for example) in your documents, you can use the orderBy method .orderBy('addedAt', descending: true)

Firestore how to query documents chronologically

I understand that unlike Firebase Realtime DB, there isn't a built in method for querying Firestore documents chronologically.
Other than adding a timestamp field to each document, is there another elegant way to achieve that?
Apparently it isn't recommended to name documents in an ascending names (Do not use monotonically increasing document IDs)

Append value to FireStore document

I am building a chat app. When adding messages to document container in Firestore, it somehow adds in the middle or somewhere else. Since I get them all and display in chat, messages are not sorted according to chronology ( last sent message appears at the beginning). So the question is how to append data in a document?
If you are not using an OrderBy clause on the query, the messages will be sorted by their Document IDs, which I am guessing in your case is the Firestore generated values. If you would like to order them by most recent, you can add a Date field to your messages and do something like
messagesCollection.orderBy("date", "desc")
See the Firestore Documentation on ordering and limiting data for more info.
Have you tried OrderBy ?
It seems with this to your firestore queries you can order according to chronology

Firestore - order documents by document field in subcollection

I have a Flutter app where users can rent items from each other with Firestore RTDB. rental documents have a chat collection that stores the chats between two users:
rentals (collection)
rental_1 (document)
chat (collection)
message_timestamp_1 (document)
message_timestamp_2 (document)
users (array, document field)
user_id_1 (String)
user_id_2 (String)
rental_2 (document)
chat (collection)
message_timestamp_1 (document)
etc.
I have a page in my app that is a listview of all the rentals that the user is involved in (simple arrayContains on the users field). Basically, I want to show all the chats the user is involved in. However, I would like to order this list by most recent chat (like any normal messaging app). Is there a way to achieve this without having to store and update a lastUpdated field in the rental document (thus creating two writes each time a message is sent)?
Adding that lastUpdated field in the rental document and querying the latest rental documents in which a user is involved using array-contains and ordering by lastUpdated field will solve your problem. As you have mentioned this will cost you two writes per message and can lead to a billing trap as a lot of messages can be expected in the chats sub-collection.
Alternatively you can create the chats collection as a top-level collection with a field rentalId so that you can query on this top-level collection to show the recent rental chats the user is involved in. This will eliminate the two writes you have to perform when writing a single message to the firestore.
Hope that helps.

Firestore: Order by sub-collection field

First of all, this is not a regular question. It's little complicated.
App summary
Recipes app where users can search recipes by selected ingredients (collection ingredients exists in firestore db). I want to store for every ingredient statistics how much did users search with that selected ingredient, so I can show them later at the top ingredients which they used mostly for searching recipes.
This is how my collection looks like:
http://prntscr.com/nlz062
And now I would like to order recipes by statistics that created logged in user.
first = firebaseHelper
.getDb()
.collection(Constants.INGREDIENTS_COLLECTION)
.orderBy("statistics." + firebaseHelper.getCurrentUser().getUid() + ".count")
.limit(25);
If logged in user hasn't yet searched recipes with ingredients, then it should order normally. Anyway the query above is not working. Is it possible this use case to be done with Firestore.
Note: Statistics may exists or may not for logged in user, it all depends on his search.
You can't query and documents by fields that don't immediately exist within the document. Or, in other words, you can't use fields documents within subcollections that are not in the named collection being queried.
As of today (using the latest Firestore client libraries), you could instead perform a collection group query to query all of the subcollections called "statistics" for their count field. However, that will still only get you the statictics documents. You would have to iterate those documents, parse the ingredient document ID out of its reference, and individually get() each one of those documents in order to display a UI.
The collection group query would look something like this in JavaScript:
firestore
.collectionGroup("statistics")
.where(FieldPath.documentId())
.orderBy("count")
.limit(25)
You should be able to iterate those results and get the related documents with no problem.