database design - What is the best data structure to store this data on mongoDB? -
i'm designing news publishing website. want allow users pick favorite news , store them in list. time user wants show favorites, news should sorted date , shown.
the structure store this:
favorite_table = { user_id, data }
all news stored in data field this:
[{date:news_id},{date:news_id},{date:news_id]
since values sorted date, structure store news? have append data whenever arrives. structure suitable purpose or there better structure?
your structure looks correct. storing news_id in data field , that's smarter implementation because storing complete document cause lot of moving , writing mongodb every time new news added favorites user.
reason, know, new document created on every update. reference: http://docs.mongodb.org/manual/reference/method/db.collection.save/#upsert.
also think business rule should restrict number of news items can set favorite user. allowing list grow indefinitely not idea embedded design.
in case want unlimited number of favorites, better create separate collection , favorites user use $match (on user's id) , $sort (of news items) operators in aggregation query sorted list of favorite news items user.
Comments
Post a Comment