0

A query gave me the required result in MongoDB while it's returning an empty array in mongoose

db.users.aggregate([

{$match:{_id:ObjectId("5d5a8b83352b262670a4d47b")}},
   {
     $lookup:
       {
         from: "userclaims",
         localField: "_id",
         foreignField: "userId",
         as: "claims"
       }},{
       $unwind:"$claims"
       }
])

this query, when executed in mongodb terminal, yielded the right result

User.aggregate([
        {$match:{_id:mongoose.Types.ObjectId("5d5a8b83352b262670a4d47b")}},
        {
            $lookup:{
                from:"UserClaim",
                localField:"_id",
                foreignField:"userId",
                as:"claims"
            }
        },{
            $unwind:"$claims"
        }
    ])
    .exec().then((claims)=>{
        console.log(claims)
    }).catch((err)=>{
        console.log(err);
    })

this is the code that I've written in mongoose which is logging [].

2
  • Can you just pass mongoose.ObjectId instead of mongoose .Types.ObjectId? Commented Sep 19, 2019 at 6:27
  • @SunnyParekh it's throwing an error asking me to user mongoose.Types.ObjectId Commented Sep 19, 2019 at 6:40

1 Answer 1

3

Try using mongo collection name(userclaims) instead of mongoose model in $lookup from

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

2 Comments

It's a good culture to upvote an answer if it helped.
I couldn't..as I've no required reputation :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.