我在MongoDB数据库的RoR上有一些应用。我使用Mongoid映射器。型号post.rb
Mongoid查询
class Post
include Mongoid::Document
field :title, :type => String
field :text, :type => String
embeds_many :comments
end
型号comment.rb
class Comment
include Mongoid::Document
field :name, :type => String
field :content, :type => String
embedded_in :post, :inverse_of => :comments
end
在数据库这篇文章的一些评论有一个结构:
{
"_id": ObjectId("4ecbeacf65430f0cef000003"),
"comments": {
"0": {
"name": "my name",
"content": "example content",
"_id": ObjectId("4ecbead365430f0cef000005")
},
"1": {
"name": "some name",
"content": "example content",
"_id": ObjectId("4ecbead665430f0cef000007")
},
"2": {
"name": "some name",
"content": "example content",
"_id": ObjectId("4ecbeada65430f0cef000009")
}
},
"text": "example text",
"title": "example title"
}
而且,例如,在数据库与几个职位我的意见。 我需要找到所有帖子,其中"name": "my name"
,即我需要找到所有可编辑的帖子。
你的评论对象不应该是对象的数组吗?不是具有任意名称的子对象的对象?我会重新看看你的架构体系结构。 – Petrogad