Closed gordol closed 10 years ago
In relational database it is easy, in MongoDB you can also use two queries:
$in
query to find artist informationHowever, this is not scalable at all, but will work exactly how your algorithm. Probably a little bit less of code as well.
map/reduce might work, but MongoDB has certain scalability problems with it.
Yep, it's running that on the Mongo server currently. I had tried to simply concat() but it kept throwing back an empty array for some reason, so instead of digging deeper I elected to just use the indexOf() method. It should only be using two queries already... I believe, well, plus two more for the admin panel to count for pagination and de-reference all the ObjectIDs, but the reverse lookup is only two queries that are both running on the backend. I currently have 3 users, who are together following 281 artists, of 11,807 artists total. I'm developing on a Chromebook locally (MongoDB running locally too) and query time to get all followed artists is 474ms, and query time to get all non-followed artists is 581ms. This query wont' be user-facing, nor blocking... admin only, and backend async workers, so I'm not too-too concerned.
Thanks for your input! I hope this is able to help steer someone else in the right direction as well.
Worst case, later, if I have issues scaling, I'll probably just use pre-save hooks on the database to keep a flat list of followed artists up-to-date on the fly as artists are followed/unfollowed, or even better, I'll just break out the user->artist relation into it's own table/document something like the following, instead of storing the User's followed artists inside the User model.
class Followers(db.Document):
artist = db.ReferenceField('Artist', reverse_delete_rule = db.CASCADE)
users = db.ListField(db.ReferenceField('User', reverse_delete_rule = db.PULL))
Which should let us query very efficiently, I'd imagine.
Or I'll just move the Users/Artists into a relational DB and only use Mongo for the data from the various APIs that we're integrating. :) Anyway, thanks again.
This isn't so much an issue, as much as a question/concern. What I have now is working great, and I couldn't find any examples of this anywhere, so I thought I'd share here, and possibly get some feedback.
Basically I have Users, and Artists, and Users can follow Artists. I want to be able to filter and only show Artists that are followed.
User model has a list field of references to the Artist model.
I'm wondering if there's a better way to go about this:
Any help or suggestions are greatly appreciated! <3
Maybe something with aggregation and/or map_reduce?