3

I want to store data in this format.

{
 "_id": ObjectId(...)
 "title": "Grocery Quality"
 "comments": [
    { author_id: ObjectId(...)
      date: Date(...)
      text: "Please expand the cheddar selection." },
    { author_id: ObjectId(...)
      date: Date(...)
      text: "Please expand the mustard selection." },
    { author_id: ObjectId(...)
      date: Date(...)
      text: "Please expand the olive selection." }
 ]
}

I'm confused as to how to achieve this format for my data.

I am using mongoid; does Mongoid support Multikey Indexing?

How can use mongoid to achieve my desired format and behaviour?

Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52

1 Answers1

1

I'm not sure if I got your doubt correctly but as I can't comment I'm answering right away. If it isnt this what you asked, please explain a little bit more =)

You have your model with those fields you wrote before, I will call it Post model. For the comments on it, I would suggest you create another model callend Comment and embed it on the Post model:

class Post
  field: title
  embeds_many :comments
end

class Comment
  field :date
  field :text
  has_one :author
  embedded_in :post
end

And to index the comments on the Post model you could do:

index({ :"comments.updated_at" => 1 })
yogodoshi
  • 124
  • 2
  • 5