Pergunta

We have the following Testsnippet in Ruby

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

The problem is the "match_global" statement. We would like to match all Documents where the course_member ID is appearing in the course_member_ids array.

The above statement fails with the error: "...must be an array". This make sense to me but according to other comments on the web this should be possible this way.

Any advice? How is it possible to return the docs where the course_member id is in the array of the course_member ids?

Sample CoursePlan Object:

{
    "_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")
    ]
}
Foi útil?

Solução

Since course_member_ids is an array of course members you should test for equality. In shell syntax:

{$match:{"_id.course_member_ids":<valueYouWantToTest>}}

You don't need $in as this query is analogous to a find when you want to select documents that have a particular single value you are looking for.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top