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.
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
); 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.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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.