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 [].

Samuel
  • 612
  • 2
  • 9
  • 25
sreeya chowdary
  • 159
  • 4
  • 12

1 Answers1

3

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

Sudhakar
  • 533
  • 1
  • 3
  • 17