3

我想以这种格式存储数据。

{
 "_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." }
 ]
}

我对如何为我的数据实现这种格式感到困惑。

我正在使用 mongoid;Mongoid 支持多键索引吗?

如何使用 mongoid 来实现我想要的格式和行为?

4

1 回答 1

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 })
于 2013-03-22T05:01:01.230 回答