16

I have two collections, a collection with users and a collection with groups. In groups the documents look like this:

{
    "_id": ObjectId("...")
    "members": [ObjectId("..."), ObjectId("..."), ObjectId("...")]
}

In the users collection the documents look like this:

{
    "_id": ObjectId("...")
    "first_name": "...",
    "last_name": "...",
    "phone": "..."
}

What I want to do is an aggregation in the users collection where I add a $lookup stage to the pipeline where I go inside the groups collection and get the group from the user based on whether the _id from the user document is inside the members array in the group document.

I have the following but I don't know how I'd proceed.

db.users.aggregate([
    {
        "$match": { "_id": ObjectId("...") }
    },
    {
        "$lookup": {
            "from": "groups",
            "let": {"_id": "$_id"},
            "pipeline": [{"$match": ???}], <- match when $$_id is inside $members
            "as": "group"
    }
]);

I know that the aggregation framework in MongoDB doesn't work with arrays and that there is an $unwind stage but I don't really know how I would combine $unwind and $match to get the result I want.

What I want:

{
    "_id": ObjectId("..."),
    "group": ObjectId("..."),
    (other fields here)
}

I use MongoDB server version 3.7.2

4
  • What's your MongoDB server version?
    – chridam
    Commented Feb 22, 2018 at 19:22
  • It's MongoDB 3.7.2 @chridam
    – Julian
    Commented Feb 22, 2018 at 19:25
  • Did you mean 3.6.2? If so, no need to do the extra filtering since the $lookup aggregation pipeline stage NOW works directly with an array (on 3.3.4 version).
    – chridam
    Commented Feb 22, 2018 at 19:29
  • What do you mean by "no need to do the extra filtering" @chridam?
    – Julian
    Commented Feb 22, 2018 at 19:47

1 Answer 1

28

You can try below aggregation one of two ways using $lookup and $lookup pipeline.

$lookup

db.users.aggregate([
  {"$match":{"_id":ObjectId(..)}},
  {"$lookup":{
    "from":"groups",
    "localField":"_id",
    "foreignField":"members",
    "as":"group"
  }},
  {"$unwind":"$group"},
  {"$unwind":"$group.members"},
  {"$match":{"group.members":ObjectId(..)}}
])

$lookup pipeline

db.users.aggregate([
  {"$match":{"_id":ObjectId(..)}},
  {"$lookup":{
    "from":"groups",
    "let":{"_id":"$_id"},
    "pipeline":[
      {"$match":{"$expr":{"$in":["$$_id","$members"]}}},
      {"$unwind":"$members"},
      {"$match":{"$expr":{"$eq":["$$_id","$members"]}}}
    ],
    "as":"members"
  }}
])
3
  • Thank you for your answer! After a bit of fiddling around I made it work for my use-case. The first $match already gave back the right document so I removed the $unwind and $match and added an $unwind and an $addFields after the $lookup stage to extract the _id field from the group and set it as a field in the user document.
    – Julian
    Commented Feb 22, 2018 at 19:53
  • Thanks for this solution. The first one working fine but second one($lookup pipeline) not working for me, is there any other reason please? Commented Nov 1, 2019 at 12:33
  • You sexy stallion thank you💋😤 Commented Jul 1, 2022 at 4:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.