mongodb - Mongo DB Schema Design -
i'm struggling find best database design app. have sql background , tend create more or less denormalised database design.
i have next problem. have collection of "articles" containing 2000 articles. each article has quite lot of information. implementing recommender system, want associate every "user" "predictedrating" each "article". in sql model using 3 tables: "articles", "users", "usertoarticle". query should follows: want associate every "article" "predictedrating" current user logged in. in sql create bring together on "article" , "users" preselecting corresponding user. having right indexes fast.
how implement in mongo way? when implement in described way, i'm forced set findone() query every article, inefficient , slow (even when using index).
have ideas? of import thing is, predicted ratings current user published.
rules of thumb
the mongodb blog has good advice on info modeling:
use embedded documents whenever possible. if subdocument read on it's own, improve not embed it. keep arrays small. if embedded array of document keeps growing, replace array of reference ids. if array of references keeps growing, seek invert references or extract references it's own collection. application-level joins still option. when using indexes , projection correctly, there shouldn't performance drop. you embed documents, updated read, if means redundant data. don't embed redundant data, if need update frequently, because outweigh read advantage. optimize info model application. needs read or written should moved closer (into fewer collections).therefore modeling document database isn't straight forwards normalizing relational info model. when mastered these rules of thumb, should read about info models in mongodb manual.
example
we need set 3 domain objects mongodb: user, article, , predicted rating. assume there lot of users , more articles. it's pretty clear shouldn't set users , articles 1 collection (bullets 2, 4, , 5). hence need decide set predicted ratings.
embedding ratings articles
as utilize case predicted ratings user, counterproductive set them articles (6). need search through articles ratings. besides if remove user need update every article.
embedding ratings users
embedding ratings users has advantage need 1 query user , rating data. you'll want add together rating every article each user, hence arrays grow much (3).
putting ratings it's own collection
therefore it's viable set ratings own collection.
{ _id: objectid("f01..."), userid: objectid("123..."), articleid: objectid("abc..."), predictedrating: 5.4 }
as said depends on quantity structure. if have few users or few articles, embedding predicted ratings simpler , faster solution.
mongodb database-schema
No comments:
Post a Comment