gpt4 book ai didi

ruby-on-rails - rails mongoid 中的多键索引

转载 作者:可可西里 更新时间:2023-11-01 09:52:51 26 4
gpt4 key购买 nike

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

{
"_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 来实现我想要的格式和行为?

最佳答案

我不确定我是否正确理解了您的疑问,但由于我无法发表评论,所以我会立即回答。如果这不是你问的,请再解释一下 =)

您的模型包含您之前编写的那些字段,我将其称为 Post 模型。对于它的评论,我建议你创建另一个模型 callend Comment 并将其嵌入到 Post 模型中:

class Post
field: title
embeds_many :comments
end

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

并索引 Post 模型上的评论,您可以这样做:

index({ :"comments.updated_at" => 1 })

关于ruby-on-rails - rails mongoid 中的多键索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15129976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com