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

别等时光非礼了梦想. 提交于 2019-12-02 00:57:21

I tackled this exact problem a while ago.

There are no joins in mongo. You have to manually take care of the join.

Your options are:

  1. Loop through each comment entry and query mongo for the user. this is what you're doing.
  2. Get all user id's from comments, query mongo for the users matching these ids, then take care to match user to comment.This is just what you did but a little more optimized.
  3. Embed the user in comments or comments in users. Wouldn't recommend this, this is probably not the right place for comments/users.
  4. Think of what set of data do you need from user when displaying a comment, and embed just this info in comment

I ended up going with the last option.
We embedded the user id, first and last name in each comment. This info is unlikely to change (possibly not even allowed to change after creation?).
If it can change then it is not too hard to tailor the update-user method to update the related comments with the new info (we did that too).
So now no join is needed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!