2016-09-20 46 views
1

我的要求收集,如何在节点js中传入输入参数以加入集合?

{ 
    "_id" : ObjectId("57e0b8cd5eeba5140b270ca8"), 
    "sent_id" : "57dfcdc21ad7cf658860926d", 
    "recieved_id" : "57dfcddf1ad7cf658860926e", 
    "status" : 1 
} 

我的资料收集

{ 
    "_id" : ObjectId("57dfcddf1ad7cf658860926e"), 
    "firstname" : "John", 
    "lastname" : "David", 
    "profilename" : "John David", 
    "email" : "[email protected]" 
    } 

在这里,我想与sent_id(集合)= _id(配置文件)加入收藏,我想下面的代码,

exports.getrequestsdetails = function (req, res) { 
    var params = req.params;console.log(params) 
    var record = db.collection('requests'); 
    var item = {"sent_id": params.id,"status":1}; 
    record.aggregate([ 
    { 
     $lookup: 
     { 
      from: "profile", 
      localField: "sent_id", 
      foreignField: "_id", 
      as: "profilename" 
     } 
    } 
]).toArray((err, result) => { 
    if (err){ return 
    } 
     if(result){ 
      response = {status:'success',data:result}; 
     } else{ 
      response = {status:'fail',data:[]}; 
     } 
     res.send(response); 
    }); 

}; 

这是我的结果,我无法看到集合已加入,

{ 
    "status": "success", 
    "data": [ 
    { 
     "_id": "57dfeb279559c37854adb92f", 
     "profilename": [] 
    }, 
    { 
     "_id": "57e0b8cd5eeba5140b270ca8", 
     "sent_id": "57dfcdc21ad7cf658860926d", 
     "recieved_id": "57dfcddf1ad7cf658860926e", 
     "status": 1, 
     "profilename": [] 
    } 
    ] 
} 

与上面的代码我遇到两个问题,

1)我比较sent_id和_id,这里的_id是对象类型,那么,如何对它们进行比较。

2)我想通过发送的ID作为params,我该怎么做?

任何人都可以建议我帮忙,因为我是新的节点和mongo.Thanks。

回答

0
  1. 约翰的ID为57dfcddf1ad7cf658860926_e_,这不等于request.sent_id 57dfcdc21ad7cf658860926_d_。因此没有加入。

  2. 你需要一个$match条款添加到聚合()调用

    $比赛:{sent_id:params.id}