0

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")
    ]
}
6
  • Not sure but I think you are looking for elemMatch. stackoverflow.com/questions/23469463/… Commented May 13, 2014 at 9:17
  • How can we use elemMatch on a "flat" array like ["123", "234"]? Because, i think, elemMatch needs a object like [{val: "123"}, {val: "234"}]. Commented May 13, 2014 at 9:25
  • Kind of a little hard given you have not presented any sample data. But the general premise is correct even though the array is "anonymous" for key values. So how about some relevant data? Commented May 13, 2014 at 9:29
  • what is the course_member_ids in the original documents? If it's an array of course member ids then just test for equality. {$match:{"_id.course_member_ids":<valueYouWantToTest>}} in shell terms. You don't need $in. Commented May 13, 2014 at 9:38
  • You're right, i've added a sample object. Thanks. Commented May 13, 2014 at 9:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.