How does firestore order documents - database

I have recently started a firestore database. I was wondering is there any order in which the documents are added? is it just in the order they are created in?

There is no inherent sort order in which documents are stored, but you can specify the sort order you want to retrieve them in through the API. The documentation also contains a full table on how data is sorted.
If you're asking about the Firestore console, that by default orders the documents on their IDs, but you can change that by clicking the filter button at the top of the list of document IDs.

Related

How to retrieve whole nested data form multiple document from firebase in flutter

How can I retrieve the all the data form each uid with their nested field and order them in according to order sent time.
Here is the structure of database
Since the order sent at field is in the Delivery array field, there can be multiple values for each document. In order to sort on a field, there must be a single value for each document.
The easiest way to allow the use-case is to introduce a new top-level field in the document, for example most recent order sent at, that you then update whenever you add a order sent at value to the Delivery array.
Once this field exists, you can order on it in your queries.

Flutter-Firestore: get Document with know id from Collection Group Query

Here's a scheme of the structure of my Firestore database
Each company doc has a users collection
In my Flutter app, I need to retrieve a User document (like the one marked with the blue star). I know the id of this document, but I don't know under which company document it is located.
What's the fastest way to retrieve such document? Can I use .collectionGroup() and then filter the query by the doc id?
You can use collectionGroup() query with a where() clause.
Firestore.instance.collectionGroup('users').where('userIdField', isEqualTo: 'USER_ID')
You can read the user's company ID from the DocumentReference of that fetched document.
As #Frank commented, using where with document ID in a collection group query will not match. You would have to store user's UID as a field as well.

Making a query to multiple collection in firestore

I have two collections where one collection is referencing another. Below is my structure:
CURRENCY_PAIR
CurrencyPair_Name
CurrencyPair_id
CURRENCY_PAIR_LIMIT
Limit_Currency_Pair_Id : CURRENCY_PAIR/0chTWgOpzolSxj590N1U <=makes a reference to the document in the CURRENCY_PAIR collection
Limit_Buy_Price_Threshhold
How can i query both collections such that when i get a currency pair, it is equal to the Limit_Buy_Price_Threshhold field that was added to the db with.
As provided on this thread, Firestore does not have a concept of server-side JOIN which is very near to what you want to achieve when you mentioned that you wanted to query both collections.
What I could think of is that you could remodel your database in a way that you can perform collection group queries. It works by putting CURRENCY_PAIR_LIMIT as a subcollection to the document in CURRENCY_PAIR.

firebase firestore nosql design for chat app with groups

I am trying to think of a way to design the firestore db in a way that is efficient.
The main issue I am having with is how I should define "groups". Lets say a user is invited to a group chat and so the client needs to retrieve the data for that group chat, should I have a "groups" collection and then find the correct group document? OR, should I have a "groups" property in the user document that has a id to reference the group to retrieve?
In SQL, having a reference in a user's groups table would be the obvious answer, but I am not sure about firestore. I don't want to look through the entire collection of groups just to find the group that the user was newly invited in. Any tips? Also, my front end is in React and I am considering using the onSnapshot method to subscribe to the collection (that seems to be the best way to have real time updates).
What i believe is best for you is this :
First have a collection, suppose you make groups, and inside that every docuent has all the group unique ids,
And inside that for every group, i.e document, you can have a collection which holds all the chats for that group and group related info , like group type, etc etc
Hope it helps. feel free for doubts

Which database model to store data in?

I am writing an application in Google App Engine with python and I want to sort users and user posts into groups. Users will be able to tag a post with a group ID and then that post will be displayed on the group page.
I would also like to relate the users to the groups so that only members of a group can tag a post with that group ID and so that I can display all the users of a group on the side. I am wondering if it would be more efficient to have a property on the user which will have all of the groups listed (I am thinking max 10 or so) or would it be better to have a property on the Group model which lists all of the users (possibly a few hundred).
Is there much of a difference here?
Your data model should derive from the most likely use cases. What are you going to retrieve?
A. Show a list of groups to a user.
B. Show a list of users in a group.
Solution:
If only A, store unindexed list of groups in a property of a user entity.
If both, same as above but indexed.
If only B, store unindexed list of users in a property of a group entity.
NB: If you make a property indexed, you cannot put hundreds of user ids in it - it will lead to an exploding index.

Resources