Express: Connect do different Mongo tables when hitting from different subdomain - webserver

I'm not sure how exactly to phrase this question the best. It was quite hard to find a specific answer. I'm a beginner in Node and Mongo and I need to handle this task.
The collections:
There are collections called Ingredients, which are available to everyone
There are collections called Recipes which are made by users by combining Ingredients
Now what I want is to create subdomains like such: communityone.domain.com, communitytwo.domain.com
What I want to do is make the Ingredients collection available to all subdomains, but make the Recipes local to the respective subdomain. So that means only communityone can view the Recipes made by users on communityone.domain.com, and likewise for community two
What is the way to do this? I'm completely lost on where to start. If it's any relevant (familiar), I'm using React and Redux, and probably Mongoose as well (new to me).

Related

Organizing MongoDB database containing different account types

I am working on a react-native app using nodejs and mongodb on the backend. In my app users are able to create multiple accounts and there are different account types (Business, Artist, Venue, etc.). For the most part, each account type has the same data and fields in the database. Things like name, location, website. But it is possible for each account type to have a couple pieces of data specific to that account. My question is, should I simply have one "Account" collection in the database that stores all accounts and has an "accountType" field to differentiate each account?
Initially I thought to do the opposite and store each account type in a separate collection, but I found it made the client code pretty messy as I found myself adding a bunch of if statements to determine things like what api endpoint to make requests to, what components to render, and what screens to navigate to, when in reality, it's really just a couple pieces of data that may change from one account type to another.
It seems like having just one "Accounts" collection with an "accountType" field will greatly simplify the code. But maybe there is something I am missing. If anyone has some insight as to which approach may be better for the situation, or some of the pros/cons of each approach, I'd really appreciate the help! Thanks!
Well, the answer clearly depends on how the documents for different types of accounts differ. But, the idea of going with a single collection is fine, also take a look at the Subset Pattern, it's will give you a fine idea, of how to divide data into different collections, depending on their usage.

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

Best practice for dealing with different groups of users in mongodb

I have 2 groups of users on my website. One is user and the other one is admin. Currently I put them in 2 different collections user and admin. My questions is:
Mongodb generates _id automatically for both collections. Since they're in 2 different collections chances are one day you created one user and one admin with the same _id right? I have a table keeping track of user / admin balances so I certainly don't want the _id to collide.
I can also put all users and admins in one collection. This way I don't have a problem but I am not sure if I should do this.
Any comments are greatly appreciated. Thanks!!
There is a very very low chance that you will have collisions.
Yes, you could put users and admins in one collection and then have a 'type' attribute that differentiates regular users and admin users. I don't know your requirements or why you have them in separate collections currently, so I can't say if you should or shouldn't do it, but it sounds like it would make some things easier.

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.

Friendship relationship with MongoDB

I'm new to MongoDB, and went with MongoMapper for some associations help.
I'm quite curious since, you see, I'm trying to establish some User<->Friend relationships, and I'm a little bit confused about the difference between Document and EmbeddedDocument.
I suppose User would be a Document, but would Friend be an EmbeddedDocument for User or a Document on its own that simply gets called (many :friends) by User?
In my preliminary design, a Friend's list would only be accessible through a User.
Thanks!
You're asking a basic "embed vs. reference" question that gets asked quite a bit when it comes to MongoDB. The answer is not always obvious.
Here's an extensive reply on a similar question. Here are the official MongoDB docs on this question.
One of the general rules:
"First class" objects, that are at top
level, typically have their own
collection.
In your case a Friend is probably a User object in itself. You probably don't want to Embed the entire Friend inside of the User. Instead, you probably want to keep a list of friends as an array inside of each User. (so probably the references)