I'm working on proof of concept app for a Twitter-style social network with around 500k users. I'm not sure how to design a schema?
Should I embed a user's membership or use a different 'membership' collection and DB reference? If I embed, then I still have to do a query to get all the user's followers. E.g.
Looking at the following user:
{"username": "alan", "photo": "123.jpg", "subscriptions": [{user Name ":" John "," Status ":" Accepted "}, {" User Name ":" Paul "," Status ":" Pending "}}}
Something like this will happen:
db.users.find ({'subscriptions.username': 'alan'});
A display point Is it any worse or worse than having a separate membership store?
Also, when the list of Subscriptions / Subscribers displayed I am currently having problems with N + 1 because the membership document tells me the target user's username but I do not need other features as if I need a profile photo. Recommendation is the practice?
Thanks Allen
First of all, you know Should you have MongoDB and any other NoSQL database Area to meet with (but know that I am a fan) is. If you are trying to normalize your data completely, then you are making a big mistake. Even in the relational database, your app gets bigger, the more your data is standardized (see by the hot potato). I have seen this time and time again, you should go mad and do not mess much, but do not worry about duplication of information at two places. One of the main points of NoSQL (in my opinion) is that your schema enters your code and is not fully in the database.
Now, to answer your question, I think your initial strategy is to do what I will do. MongoDB can keep indexes on such elements, which are arrays, so that you are looking for the friendship of the user, which will make things very fast. But in reality, the only way to really make sure is to run a test program that creates a database full of names and relationships
You can script up some input in Python or Perl Or use a file of whatever names you want to generate, and generate some relationships. Check out the list containing the last name. Download the file dist.all.last
and type some programs like:
#! / Usr / bin / env import dragon import randomly as rand = open ('dist.all.last') for name in the name = [] f: names.append (line.split () [0] ]) Name in names: numOfFriends = rand.randint (0, 1000) rels [name] = [] In the category I (numofFriends): newFriend = rand.choice (name) if newFriend! = Name: #cannot be friends with yourself rels [name] .append (newFriend) # Take relationships (i.e. rels) and write them to MongoDB
Also, a As a general note, your field names appear for a long time Remember that the name of the field is repeated with every document in that collection because you do not trust a field in any other document Can. To save space, a general strategy is to use "username" rather than "unam" like a "field" names, but it's a small thing to see great advice in the post.
Edit:
Actually, keeping in mind a bit more on your problem, I will also give another suggestion: make the indexes more efficient For example, type of membership in different areas, for example:
{username}: "alan", "photo": "123.jpg", "subscriptions": [{user " Name ":" John "," As you said above, I will do this:
"" acceptable "}, {" user name ":" Paul ", "Status": "pending"}}}
"users Name ":" Allen "," photo ":" 123.jpg "," ACC_SSubs ": [" John "]," PD_subs ": [" Paul "]}Can create an index for each type of membership, "Like many people have Paul pending?" And "How many people subscribe to Paul?" Super fast either way indexing on the values of Mongo's array is actually an epic victory.
Comments
Post a Comment