0
價值,我們有在Ruby中MongoDB的聚合:在陣列
def self.course_overview(course_member=nil)
course_member = CourseMember.last if course_member == nil
group_global = {"$group" =>
{"_id" => { "course_id" => "$course_id",
"title" => "$title",
"place" => "$place",
"description" => "$description",
"choosen_id" => "$choosen_id",
"year" => {"$year" => "$created_at"},
"course_member_ids" => "$course_member_ids"}}
}
match_global = {"$match" => {"_id.course_member_ids" => {"$in" => "#{course_member.id}"} }}
test = CoursePlan.collection.aggregate([group_global, match_global])
return test
end
以下Testsnippet的問題是 「match_global」 語句。我們希望匹配course_member_ids數組中出現course_member ID的所有文檔。
上述語句失敗,出現錯誤:「...必須是數組」。這對我來說很有意義,但根據網絡上的其他評論,這應該是可能的。
有什麼建議嗎?如何返回course_member id在course_member ID數組中的文檔?
樣品CoursePlan對象:
{
"_id" : ObjectId("5371e70651a53ed5ad000055"),
"course_id" : ObjectId("5371e2e051a53ed5ad000039"),
"course_member_ids" : [
ObjectId("5371e2a751a53ed5ad00002d"),
ObjectId("5371e2b251a53ed5ad000030"),
ObjectId("5371e2bb51a53ed5ad000033")
],
"created_at" : ISODate("2014-05-13T09:33:58.042Z"),
"current_user" : "51b473bf6986aee9c0000002",
"description" : "Schulung 1/Elektro",
"fill_out" : ISODate("2014-04-30T22:00:00.000Z"),
"place" : "TEST",
"title" : "Schulung 1",
"updated_at" : ISODate("2014-05-13T09:33:58.811Z"),
"user_ids" : [
ObjectId("51b473bf6986aee9c0000002"),
ObjectId("521d7f606986ae4826000002"),
ObjectId("521d8b3f6986aed678000007")
]
}
不知道,但我認爲你正在尋找elemMatch。 http://stackoverflow.com/questions/23469463/mongo-db-querying-a-nested-array-of-objects/23469748#23469748 –
我們如何使用elemMatch像「[123」,「 234" ]?因爲,我認爲,elemMatch需要一個對象,如[{val:「123」},{val:「234」}]。 – patrickkeller
有點難的給你沒有提供任何樣本數據。但是,即使數組對於關鍵值是「匿名」的,一般前提也是正確的。那麼一些相關數據呢? –