MongoDb 3.6 lookup with _ID as a foreign key

98 views Asked by At

I an to fetch data from "udatas" schema with lookup "productdata". I quess this works with mongodb 4.x but not with 3.6. Any Ideas?

Udata schema

{
"_id" : ObjectId("5f6357500153c4d59b38f1e7"),
"uId" : "112345",
"pId" : "5f63436f3d0e2bd390324847",
"sizeId" : "5f63483340421ed478f9d44e",
"general" : "4",
"length" : "5",
"width" : "3",
"height" : "5",
"comment" : "xxxxxx",
"__v" : 0
}
  

and here is "products" schema

{
"_id" : ObjectId("5f63436f3d0e2bd390324847"),
"brand" : "Brand A",
"name" : "product x",
"model" : "",
"color" : "Black",
"image" : "image.jpg",
"sku" : "abc12345",
"__v" : 0
}

This I've tried, however I get empty product?

db.getCollection('udatas').aggregate([
    {
        $lookup: {
            from: "products",  
            localField: "pId",
            foreignField: "_id",
            as: "product"
        }
    },
    { 
        $project: {
            "pid" :1,
            "uId":1,
            "comment": 1,
            "product":1
                
            }
    }
]) 
    

Thanks in advance

0

There are 0 answers