Junaid Syed

Junaid Syed

  • NA
  • 485
  • 127.7k

How to join 2 collections based on ID's

Jun 25 2018 1:23 AM
Hi iam trying to join 2 collections based on parameters I need to pass Property ID as parameter and get that perticular property details from property collection but iam getting whole data from 2 collections.So how can i pass parameter in the aggregate joins in Node js
my code is some thing like this
 
  1. exports.sample = function(req, res){  
  2.     var input = req.body;  
  3.     var newValues = {UserID:input.UserID,IsFavorite:input.IsFavorite,PropertyId:input.PropertyId}  
  4.     console.log(input);  
  5.     MongoClient.connect(url, function(err, db){   
  6.         if (err) throw err;  
  7.           var dbo = db.db("abc");  
  8.           dbo.collection('CLC_Isfavorite').aggregate([  
  9.               {  
  10.                   $lookup:   
  11.                   {  
  12.                       from: 'CLC_Property',  
  13.                       localField:'PropertyId',  
  14.                       foreignField:'PropertyId',  
  15.                       as:'propertyfavorites'  
  16.                     }  
  17.                 }  
  18.                 ]).toArray(function(err,result){  
  19.                     if (err) throw err;  
  20.                     console.log(JSON.stringify(result))  
  21.                     res.json({status : 'success', message : 'Records found', result : result});  
  22.                     db.close();  
  23.                 })  
  24.     });  

 In the above code i need to get  property details based on property id which i pass where can i define the parameter property id in above code.Can anyone suggest me any idea thank you in advance

Answers (1)