Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
MongoDB - How to verify if a nested array item is contained in another nested array item on the same document?

The idea here is to return an array of documents of the users' followers with the information if this user is a friend of that follower or not.

So far I have:

db.getCollection('users').aggregate([
    { $match: { _id: ObjectId("588877d82523b4395039910a") } },
    { $lookup: {
            from: 'users',
            localField: 'followers',
            foreignField: '_id',
            as: 's_followers'
        }
    },
    { 
        $project: {
            "s_followers._id": 1,
            "s_followers.isFriend": {
                $in: ["s_followers.id",
                      { $setIntersection: ["$friends", "$followers"] }
                ]}
        }
    }
])

But the "s_followers.id" used in the $in operator doesn't seem to retrieve the _id information from the follower, so it always returns false.

When I use a ObjectId directly, I got the result I want:

"s_followers.isFriend": {
                    $in: [ObjectId("588877d82523b4395039910a"),
                          { $setIntersection: ["$friends", "$followers"] }
                    ]}

But I really need this ID to be a reference to the follower _id.

Expected result would be something like:

{
    "_id" : ObjectId("588877d82523b4395039910a"),
    "s_followers" : [ 
        {
            "_id" : ObjectId("5888687e56be8f172844d96f"),
            "isFriend" : true
        }, 
        {
            "_id" : ObjectId("5888ca27d79b8b03949a6e8c"),
            "isFriend" : false
        }
    ]
}

Thanks for your help!

UPD: A different approach (maybe easier), would be to use the ID of the user that I have (the one used on $match), but I would still need to get the reference for the follower's follower array

db.getCollection('users').aggregate([
    { $match: { _id: ObjectId("588877d82523b4395039910a") } },
    { $lookup: {
            from: 'users',
            localField: 'followers',
            foreignField: '_id',
            as: 's_followers'
        }
    }, {
        $project: {
            "firstName": 1,
            "s_followers._id": 1,
            "s_followers.firstName": 1,
            "s_followers.followers": 1,
            "s_followers.isFriend": { $in: [ObjectId("588877d82523b4395039910a"), "$s_followers.followers"] }
        }
    }
])

UPD2: The user data structure (the part that matters)

{
  followers: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
  friends: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

FOR VERSION 3.4.0+

Ok, just got it, I'll post here the code and my understanding of it:

db.getCollection('users').aggregate([
    { $match: { _id: ObjectId("588877d82523b4395039910a") } },
    { $lookup: {
            from: 'users',
            localField: 'followers',
            foreignField: '_id',
            as: 's_followers'
        }
    }, {
        $project: {
            "firstName": 1,
            "s_followers._id": 1,
            "s_followers.firstName": 1,
            "s_followers.followers": 1,
        }
    }, { 
        $unwind: "$s_followers"
    }, {
        $project: {
            "firstName": "$s_followers.firstName",
            "isFriend": { $in: [ObjectId("588877d82523b4395039910a"), "$s_followers.followers"] }
        }
    }
])

My understanding of it:

  1. $match: match the user I'm intended to get the followers of.
  2. $lookup: found each follower detail
  3. $project: select the information I want to return, also get the follower list of each follower
  4. $unwind: create a different document for each follower
  5. With the array unwinded, I can refer to the follower's followers array, and find my object id in it :)
over 4 years ago · Santiago Trujillo Report

0

In my example use followers friends list to check current user is friend or not. as {$arrayElemAt:["$s_followers.friends",0]} if want to find in followers then can use "$s_followers.followers"

You can try it.

db.getCollection('user').aggregate([
    { $match: { _id: ObjectId("5714d190e6128b7e7f8d9008") } },
    {$unwind:"$followers"},
    { $lookup: {
            from: 'user',
            localField: 'followers',
            foreignField: '_id',
            as: 's_followers'
        }
    },
    {$project:{
        firstName:1,
        s_followers:{$arrayElemAt:["$s_followers",0]},
        isFriend:{$cond:[{
                $anyElementTrue:{
                  $map: {"input": {$arrayElemAt:["$s_followers.friends",0]},
                    "as": "el",
                    "in": { "$eq": [ "$$el", "$_id" ] }
                  }
                }
              },true,false]}
        } 
    },
    {$group:{
        _id:"$_id",
        s_followers:{$push:{_id:"$s_followers._id",isFriend:"$isFriend"}}
        }
    }
])
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!