How should I be storing one to many collections in mongodb - 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.

Related

How to paginate data from multiple Firestore collections in 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.

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!

Firestore data model for posts and comments

I am currently watching a how-to create an instagram clone for Swift and want to understand the data model for the comments.
What is the purpose of using a model for the comments like:
post-comment (key = post-id) and comments
over something like this, where every comment has the post-id in it?
Without knowing what exactly they're building, and the types of queries they need to support for the app, one can only guess that this post-comments collection satisfies the need for a query to find out which comments are a part of which posts, while still allowing queries that search all posts or all comments. You should find the part of the tutorial that queries this collection to find out what it's trying to do.
This tutorial might be kind of old, because this sort of thing would be a little bit easier to express today using collection group queries.

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).

Using as a graph database for finding "friends" of "friends" in MongoDb

I have been investigating a graph database and I have found neo4j and although this seems ideal I have also come across Mongodb.
Mongodb is not an official graph database but I wondered if it could be used for my scenario.
I am writing an application where users can have friends and those friends can have friends etc, the typical social part of a social network.
I was wondering in my situation whether Mongodb might suffice. How easy would it be to implement or do I really need to focus on REAL graph databases?
I do notice foursquare are using Mongodb so I presume it supports their infrastructure.
But how easy would it be to find all friends of my friends that also have friends in common, for example?
Although it wouldn't be impossible, MongoDB would not be a good fit for this scenario.
The reason is that MongoDB does not do JOINs. When you need a query which spans multiple documents, you need a separate query for each document.
In your example, each user document would have an array with the _id's of their friends. To find "all friends of the friends of UserA who are also friends of UserB" would mean that you would:
find userA and get his friends-array
find all users in that array and get their friend-arrays
find all users in these arrays who have UserB in their friends-array
These are three queries you have to perform. Between each of these queries, the result set has to be sent to the application, the application has to formulate a new query and send it back to the database. The result-set returned from the 2nd query can be quite large, which means that the 3rd query could take a while.
tl;dr: Use the right tool for the job. When your data is graph-based and you want to do graph-based queries on it, use a graph database.
You likely want an actual graph database as opposed to MongoDB. Try using the TinkerPop graph technology stack to get started. Using Blueprints (which is like JDBC for graphs) you can see the performance of MongoDB as a graph (using the Blueprints MongoDB implementation) versus Neo4j, Titan, or any number of other graph implementations.