[英]Mongoid and Mongodb querying
I have a data strcuture like this 我有这样的数据结构
Courses
{
"name" : "Course1",
"student_id"
"subjects" :
[{
"level" : "1",
"short_name" : "Maths",
"topics" : [{
"name" : "Algebra 101",
"week" : "1",
"submission_week" : ISODate("2013-07-28T00:00:00Z"),
"correction_week" : ISODate("2013-07-28T00:00:00Z")
},
{
"name" : "Algebra 201",
"week" : "1",
"submission_week" : ISODate("2013-07-28T00:00:00Z"),
"correction_week" : ISODate("2013-07-28T00:00:00Z")
}
]},
{
"level" : "2",
"short_name" : "Chem"
}
]
}
Using Mongoid I am trying to retrieve all topics. 我正在尝试使用Mongoid检索所有主题。
I have tried all sorts of queries but cannot seem get it. 我尝试了各种查询,但似乎无法理解。
eg I don't understand why this doesn't work? 例如,我不明白为什么这行不通?
Topic.where( name: "Algebra 101", 'subject.short_name' =>"Maths", 'subject.course.name' =>"Course1")
Can I query like this? 我可以这样查询吗?
My ruby code is 我的红宝石代码是
class Course
embeds_many :subjects
class Subject
embedded_in :course
embeds_many :topics
class Topic
embedded_in :subject
A far as I know it's only possible to make such a query on the top-most model (in this case Course
); 据我所知,只能对最顶层的模型(在这种情况下为Course
)进行这样的查询; I have not seen a way to directly obtain an embedded model like that yet. 我还没有看到一种直接获得这种嵌入式模型的方法。
So this should work, but only gives you the Course
: 所以这应该有效,但只会给您Course
:
Course.where(name: 'Course1', 'subjects.short_name' => 'Maths', 'subjects.topics.name' => "Algebra 101")
And this is the (unfortunately rather ugly) query that should give you what you want: 这是(不幸的是很丑陋的)查询,应该可以为您提供所需的信息:
Course.where(name: 'Course1').subjects.where(short_name: 'Maths').topics.where(name: 'Algebra 101')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.