How to paginate data from multiple Firestore collections in Flutter? - flutter

I want to create a screen in my Flutter app to display all financing details that relate to a specific user. Each kind of financial entry has its own collection on Firestore.
To elaborate more, I have a purchases collection and a transactions collection. A user should be able to see both of these collections together in a single view sorted by date or money-spent, which means (I think) I have to "join" these two collections (like in SQL) and display the result.
The question now remains how can I paginate this data? As far as I know, I can't "join" collections in Firestore, and as far as I have searched I can't really paginate data from multiple collections simultaneously.

If you want to show a paginated view of the data from two collections, you will need to load the full page of data from both collections, merge them in your application code, and discard any data that is more than the page size you want to show.
In cases like this always consider whether you can instead create an additional collection where you store the data from both collections, and read from there. While data duplication is frowned upon in relational data modeling, it is very common in NoSQL databases.
To learn more about the topic, I recommend reading NoSQL data modeling and then watching Todd's excellent Getting to know Cloud Firestore.

Related

How to store one-time data in a MongoDB database?

I am building a personal work/career portfolio web app project, and plan on using MongoDB for my database. (I plan to build the project using MERN stack.) Most of my data is not one-time data (such as education, and work experiences), however I have a few pieces of data (such as my personal summary (the content for my "About Me" section), and skills summary) that are one-time only data (I think "single instance" might be a better fitting term). I would like to store all of the data in a database, and set up an admin-end to manage and edit the data. However, I am not sure how to go about storing the one-time data in my MongoDB database.
One idea I had was to create a collection solely for the one-time data, and only allow the user (me) to update and read the documents in the collection. Another idea I had was placing all of my portfolio data into a single collection called "entries", and giving each "entry" a type (such as "Education", or "Personal Summary"). Then when I retrieve the data from the collection I would gather all the documents with the same value in their type field together. I was thinking of storing each of the types as a constant on my server. However, my biggest concern with both ideas is if they would be considered bad practice of not.
I would be very appreciative if anyone has any advice on how to solve this problem.
I had implemented this a while back on one of my small projects, and again after discussing it over with some professionals I'm in contact with, they said that the best approach would be to create a collection with a single document that contains all the information, like the links, about, etc...
One more thing I, was suggested is that we could use Redis solely for the purpose of storing this type of information as well.
Something that I implemented a long time back similar to the one collection, single doc approach: https://github.com/codelancedevs/Sundar-Clinic/tree/local-backend/src/api/app
Working on a similar approach here: https://github.com/kunalkeshan/Cam-O-Genics-Backend
Hope this is of some help, I'm still learning as to what might be the best approach. Open to any suggestions out there!

How do I get the number of subcollectors in Firestore?

I'm making a whiteboard app using Flutter and Firestore.
This whiteboard app creates a room first, then creates pages, and then draws inside the page.
Firestore has the following configuration:
I need to be able to get the number of subcollections in Firestore to get the number of pages.
How do I get the number of these subcollections(pages)?
You can't get all collection from one doc in dart.
Retrieving a list of collections is not possible with the mobile/web client libraries. You should only look up collection names as part of administrative tasks in trusted server environments. If you find that you need this capability in the mobile/web client libraries, consider restructuring your data so that subcollection names are predictable.
Here is the document link.
There is no function that can return the number of sub-collections that exist within a document. If you need such a count, you need to create this mechanism yourself. But it's pretty simple, you can create a document in which you can increment/decrement a numeric value, each time a new sub-collection is added or deleted from the document.

How should I be storing one to many collections in mongodb

Not sure if collection is the right word but I'm trying to say whatever a table would be in MongoDB
I'm planning on making a switch from MySQL to MongoDB and have been reading up on it but one thing I can't seem to find much coverage on is one-to-many or many-to-many collections.
So say I have a forum collection, forum collection has many posts, as well there is a user collection which has many posts (posts is shared between forum and user so that you can see a users profile and see their posts as well when you visit the forum it will populate recent posts)
What would be the way I should be associating these, should I directly insert the post into both User and Forum collection that way I can just query the user and get their posts, or should I store the posts in forums with a userid and then query the forum collection for posts by a certain userid
Sorry for poor formatting as I am on mobile. This isn't specific to a forum it is just an example on the proper way to be storing One-To-Many collections. Thanks!
First, ask yourself why are you moving to MongoDB, MongoDB by definition is not a relational DB. If you want to achieve query performance, then the answer is to store the data twice and maintain it via code (when stuff are updated, update twice etc...).
If you don't have performance issues, consider keep it Mysql, cause holding relational data in MongoDB is less recommended since you don't really have transactions.
So there is no right or wrong, it's depends on what is the problem you are trying to solve.

Dilemma with the data model using MongoDB

I am working on an application on which we'll have users and videos.
It's a n-n relationship, a user can be related to several videos, and the same video can be related to several users.
I decided to go with mongoDB on the implementation, though I wasn't familiar with this technology at first, so I run into a problem regarding the document data model (in contraposition with the entity-relation data model).
In this application I'll need to access frequently the videos that are somehow related to a certain user. From this point of view, it would be logical to embed the document 'video' in the document 'user'.
But, I will also need frequent access to video collections, regardless of the users related to them. From this point of view, it seems the data model would be better designed if the the users related to the video were embedded inside its document.
Both designs make sense, and solve a problem, but make the remaining problem quite hard to solve; I would have to perform complex, inefficient queries to actually be able to get both functionalities with any of those two designs.
Right now I think the best decission would be to implement it the same way I would in a relational database (with two different documents for users and videos, and an intermediate document that allows me to know the relations between those two).
I'm really not sure that is the way this problem should be solved in mongoDB, so I would like to ask for advice regarding the data model design.
Thanks in advance.
Do both.
While redundancy should be avoided in a relational database, the same is not true for a document-oriented database. When you have no JOINs, you need to make sure that every common query can be fulfilled with documents from a single collection. Redundancy is usually the only way to achieve this.
The downside is that you now need two queries to update the relation, because both the video and the user document need to be updated. But that's a small price to pay, especially considering that updates are usually not as performance-critical as reads (you can perform them in the background while faking the result on the frontend for the user who requested the update).

MongoDB permissions-based modelling problem

I'm trying to model a simple, experimental app as I learn Symfony and Doctrine.
My data model requires some flexibility, so I'm currenty looking into the possibility of using either an EAV model, or document store in MongoDB.
Here's my basic requirements:
Users will be able to store and share their favourite things (TV prog, website, song etc).
The list of possible 'things' a user can store is unknown. For example, a user may want to store their favourite animal.
Users can share their favourite things with other users. However, a user can decide what he / she shares with each other user. For example, a user may share their favourite movie with one user, but not another.
A typical user will log in and view all the favourite things from their list of friends, depending on what his friends have decided to share. The user will also update their own favourite things, which will be reflected when each other users views their own profile. Finally, the user may change which of his friends can see what of his favourite thing.
I've worked a lot with Magento, which uses the EAV model extensively. However, I'm adding another layer of complexity by restricting which users can see what information.
I'm instantly drawn to MongoDB as the schemaless format gives me the flexibility I require. However, I'm not sure how easy (or efficient) it will be to access the data once it's saved. I'm also concerned about how changes to the data will be managed, e.g. a user changes their favourite film.
I'm hoping someone can point me in the right direction. This is purely a demo app I'm building to further my knowledge, but I'm treating it like a real-world app where data access times are super-important.
Modelling this kind of app in a traditional relational DB makes me sweat when I think about the crazy number of joins I'd need to get the data for one user.
Thanks for reading this far, and please let me know if I can provide anymore information.
Regards,
Fish
You need to choose a model based on how you need to access the data.
If you just need to filter out some values when viewing the user profile, a single document for each user would work quite well, with each favorite within that having a list of authorized user/group IDs that is applied in the application code. Both read and write are single operations on a known document in this case, so will be fast.
If you need views across multiple profiles though, your main document should probably be the favorite. You'll need to set up the right indexes, but performance shouldn't be a problem.
Actually, the permissions you describe don't add that much complexity to an EAV schema - as long as attributes can have multiple values the permissions list is just one more attribute.