开发者

Representing a "ask to answer" feature on Mongodb

开发者 https://www.devze.com 2023-03-12 04:08 出处:网络
The \"ask to answer\" feature on quora is a similar technical issue to the one i am facing. Essentially, one user(suggester) asks another user(suggested) to answer a question(question).

The "ask to answer" feature on quora is a similar technical issue to the one i am facing.

Essentially, one user(suggester) asks another user(suggested) to answer a question(question).

What would be the best way to represent this schema in mongodb?

I thought about having something like this:

User class stores an array called suggested questions, which stores a pair of values
Each value is "suggest开发者_StackOverflower_userid | question_id"

This would be efficient from a retrieval pov, but seems to break proper schema design.

What would be a good way to do this?


Essentially, one user(suggester) asks another user(suggested) to answer a question(question).

not sure i understand the problem but a collection

{ suggester:..., suggested:..., question:...}

might be fine -- suggester and suggested can be indexed.


I have similar schema in my app, i have used different collection called suggestion

it looks likee this (Ruby mongoid class)

class Item
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

  field :Suggested_id, :type => BSON::ObjectId
  field :Question_id, :type => BSON::ObjectId

  embedded_in :user
end

this will ultimately translated to

User
 |
  _  Name
  _  Other fields
  _  Suggestions (embedded doc)
        - Suggested_id
        - Question_id

If you think the suggestion limit may exceeds the mongo doc size, then you can change the embedded doc to DbRef document, so the each user doc will have one associated suggestion documents. so the single suggestion document will have the user_id and all the suggestions made by the user as a embedded docs (items)

 Suggestion
   |
    _ user_id
    _ items
        |
          _ suggested_id
          _ Questionid
        |
          _ suggested_id
          _ Questionid

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消