How to store User's liked items?

Continuing from my latest thread: Should Areas (country, province, city, district) fields inside Listing collections stamped as index? - Working with Data - MongoDB Developer Community Forums

Think of each user can like many posts or products that they want, this is a Many to Many relationships. In SQL we could have a table named likes or user_likes to store the user’s liked items.

Remember that, of course, a user can store thousand or perhaps millions.

But how to accomplish it in MongoDB? There are 3 scenarios or schemas that I can think of;

#1 The ??? Pattern
I don’t know what or how to name it, but with this pattern, we could have a collection that automatically creates 1 document for each user. This document would store all the liked items’ ID by the user.

The naming convention for every ID within this collection would be user ID, therefore whenever we would like to get or compare items whether they’re liked by the user, we would just have to query this particular document that belongs to a user.

#2 Many to Many documents
Think of this collection is a pivot table in the SQL world, thus every document here are a pivot document that similar to pivot rows SQL.

Within every document, we will store the user_id and the item_id.

#3 Document references
As stated here: Model One-to-Many Relationships with Document References — MongoDB Manual

Within the items collection, we would store all the user ID who likes the item.

Thank you everyone at MongoDB.

Hi @ibgpramana,

Since this many to many relationship is going to be masses to masses I would not embed it in any way.

I would use a relationship collection to avoid unbound arrays antipatterns.

So the collection of users_likes will reference a userId to an itemId per document. However, I would not only specify the item id here but also some immutable information I need to show my list:

{ userId : ...,
  ItemId : ...,
  Details : {
    ItemName : ...,
    ItemDiscription : ...,
    DateAdded : ..., 
    PicUrl: ....
 ....
}
}

Indexing userId and Details.DateAdded : -1 might help you search for user specific items sorted by recently added.

And to show an initial list will not require another access to Item collection and will be effective.

Let me know if you have any questions.

I suggest to read those articles:

Thanks
Pavel

1 Like

Thank You, Mr Pavel.

Your reply really helps!

I’ll surely create indexes for user_id and item_id, and by the way, the reason why we’re putting the details object and its field as sub-document, is so that we don’t need another query access to each item document right?

(as you stated: And to show an initial list will not require another access to Item collection and will be effective.)

If we don’t, then we’d have to always run query access to each liked item just for getting their names, description, and picture URL.

Thank you!

Hi @ibgpramana

I’ll surely create indexes for user_id and item_id , and by the way, the reason why we’re putting the details object and its field as sub-document, is so that we don’t need another query access to each item document right?

Well yes. If you have all the data you need to show your list in UI you can only query this relationship collection.

db.users_likes.find({user_id : USER_ID}).sort({"Details.DateAdded" : -1})

If more details are needed you can implement a click on item and show more details by doing a single document query to items based on ItemId:

db.items.find({_id : ITEM_ID});

Thanks,
Pavel

1 Like

Thank you, Mr Pavel.

#Solved.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.