Firestore - how to do a "reverse" array-contains? - google-cloud-firestore

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"]);

Related

how to fetch all the documents below a specific doc from firebase flutter

how can we fetch all the document after a specific document from firebase flutter ? what I mean is for example we have 10 documents with respective id and order[1,2,3,4,5,6,7,8,9,10] in firebase I want to fetch documents from [1 to 5]once then [6 to 10] in next call .some thing for state management loading a certain number of documents once then another range of documents in next call and so on and reflect in User Interface accordingly.
please help me in this regards..
Firestore has a startAfterDocument method that can be used to query collections after a certain index based on the document. You can first query the collection and set limit to the number of documents retrieved using
firestore.collection('example').limit(5).get(). With this result you can store the last document and use it for the next queries as such:
firestore.collection('example').startAfterDocument(lastDocumentInPreviousQuery).limit(5)
Just make sure you always store the last document that was retrieved in a query to ensure you get the correct result.

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)

Firestore check if array in specific document contains a value

I would like to check if the array "devices" which is in the document "SOjTxEjMwb71isiiPnR8" contains the value "AE0EBAFA-1560-404B-A3DA". I just want to know this specific array in the specific document. Because it can be that the same value in the same array occurs in another document. How do I have to do this in Swift? Thanks a lot for your help.
You're looking for the array-contains operations, which in Swift looks like this:
devicesRefRef
.whereField("devices", arrayContains: "AE0EBAFA-1560-404B-A3DA")
You can then get the results once or listen for realtime updates. You'll note that all links I provide are to the relevant sections of the documentation, so I recommend spending some time there.

Sort documents in firestore collection chronologically

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({});

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