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
Related
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.
I am importing csv data to firestore by parsing it. The csv is making documents randomly without following the date's chronological order e.g 2019's data first and 2022 or latest data at last. I want to show the data chronological wise on the app side or even if possible on the javascript side of firebase while parsing I'm ready to make changes.
Format is dd-mm-yyyy I've tried orderBy but it didn't worked.
Note that there's Date field as well in my every document and I can't use Timestamp cause data is coming from csv.
I'm attaching image of my app and firestore document id and collection to get an better idea of what I want to achieve.
Document IDs are strings, so they use lexicographical sort order. If you want them to also be sorted chronologically, you should use a date format that fits those needs. For example ISO-8601: 2022-09-15. In this format, ordering by document ID will put the documents in the order of the date.
How can I filter the results on the Firestore Console by a timestamp field?
On the collection users, we have a field createdOn of type timestamp. If I want to filter the collection by field, I get the following dialog
I have tried entering the date as string
2019-09-15
2019-09-15T00:00:00Z
I have also tried using a timestamp as number in millis and seconds
1568505600000
1568505600
When looking at the requests sent to Firestore, the structured query uses a field filter with a value of corresponding to either stringValue or integerValue, but I think the timestampValue would be the right thing.
I do not want to adapt the query in the console and build my own requests. I know that there is always the option to sort the documents in the collection and then scroll to where it's interesting, but this will load all documents that are not relevant.
Is there a simple way to do what I want?
There is a new query builder tab in the console (I do not know when this was introduced, but I assume during the Firebase Summit 2022). For the query above, this would look like this
It even has a timestamp type in the select list.
Is there a way to sort documents in collection chronologically when they are created? Currently, they are all over the place. For example, in To-Do app, when you add new item to collection, it should display at the bottom, last, not somewhere in the middle.
You will need to define an order based on some data in the document, and order your queries based on that field.
The typical solution for time-base order to make sure your documents all contain a timestamp field that you can use to sort them. When you call add() (or other methods to update data), you can tell Firestore to use the current time using FieldValue.serverTimestamp():
collection(...).add({
..., // your other fields
createdOn: FieldValue.serverTimestamp()
})
Then you can use that field to sort when querying with orderBy():
collection(...).orderBy('createdOn')
Try using DateTime.now() for the document ID. This should put the collection in chronological order.
For example:
Firestore.instance.collection('Posts').document(DateTime.now().toString()).setData({});
I have a collection users in firestore where one of the fields is "contact_person".
Then I have an array arrNames {'Jim', 'Danny', 'Rachel'} in my frontend and I want to get all users that have either of these names in their "contact_person" field.
Something like where("contact_person" IN arrNames)
How could I do that?
This is currently not possible within the Firestore API. You will need to do a separate get for each document, unless the names happen to be in a single contiguous range.
Also see:
FireStore Where In Query
Google Firestore - how to get document by multiple ids in one round trip?
It is now possible (from 11/2019) https://firebase.googleblog.com/2019/11/cloud-firestore-now-supports-in-queries.html but keep in mind that the array size can't be greater than 10.
db.collection("projects").where("status", "in", ["public", "unlisted", "secret"]);