mongodb - Best way to store/get values referenced from a list in Mongo/RectiveMongo? -


i have quite common use case - list of comments. each comment has author.

i'm storing reference comment author using reference, since author can make multiple comments.

now i'm working reactivemongo , want try keep database access asynchronous, in case, don't know how. asynchronous access database, comments, each comment have author, , until way know loop through comments , user synchronously:

val useroption:option[jsobject] = await.result(userscollection.find(json.obj("id" -> userid).one[jsobject], timeout) //... 

other that, could:

  • get each user asynchronously have introduce functionality wait until user fetched, in order return response, , code become mess.

  • store complete user object - @ least need comment (picture, name , such) in each comment. redundancy become troublesome manage, since each time user changes (relevant data stored in comments) have go through comments in database , modify it.

what correct pattern apply here?

i tackled exact problem while ago.

there no joins in mongo. have manually take care of join.

your options are:

  1. loop through each comment entry , query mongo user. you're doing.
  2. get user id's comments, query mongo users matching these ids, take care match user comment.this did little more optimized.
  3. embed user in comments or comments in users. wouldn't recommend this, not right place comments/users.
  4. think of set of data need user when displaying comment, , embed info in comment

i ended going last option.
embedded user id, first , last name in each comment. info unlikely change (possibly not allowed change after creation?).
if can change not hard tailor update-user method update related comments new info (we did too).
no join needed.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -