如何使用mongo的聚合返回响应?

时间:2017-04-05 17:31:38

标签: node.js mongodb mongoose aggregation-framework

我从mongodb开始,我使用聚合函数,它将最后一个元素的最后一个用户放入sampleStatus数组中。 (我的意思是添加到sampleStatus的最新记录)

我有这样的样本集合:

{
    "_id" : ObjectId("58d6cbc14124691cd8154d72"),
    "correlativeCode" : "CSLLPA53E20M017W",
    "registrationMethod" : "taken",
    "originPlace" : "SOMEPLACE",
    "temperature" : 16,
    "sampleStatus" : [ 
        {
            "nameStatus" : "status1",
            "place" : "place1",
            "rejectionReason" : "Nothing",
            "user" : "user1",
            "_id" : ObjectId("58d6cbc14124691cd8154d73")
        }, 
        {
            "nameStatus" : "status2",
            "place" : "place2",
            "rejectionReason" : "Nothing",
            "user" : "user4",
            "_id" : ObjectId("58d6cbc14124691cd8154d73")
        }, 
        {
            "nameStatus" : "status3",
            "place" : "place3",
            "rejectionReason" : "Nothing",
            "user" : "user3",
            "_id" : ObjectId("58d6cbc14124691cd8154d73")
        }, 
        {
            "nameStatus" : "status4",
            "place" : "place4",
            "rejectionReason" : "Nothing",
            "user" : "user1",
            "_id" : ObjectId("58d6cbc14124691cd8154d73")
        }, 
        {
            "nameStatus" : "status5",
            "place" : "place5",
            "rejectionReason" : "Nothing",
            "user" : "user5",
            "_id" : ObjectId("58d6cbc14124691cd8154d73")
        }
    ]
}

这是我使用的功能:

db.collection.aggregate([
    { "$match": { "correlativeCode": "CSLLPA53E20M017W" } },
    { "$redact": { 
        "$cond": [
            { "$eq": [ 
                { "$let": {
                    "vars": { 
                        "item": { "$arrayElemAt": [ "$sampleStatus", -1 ] }
                    },
                    "in": "$$item.user"
                } },
                "user5"
            ] },
            "$$KEEP",
            "$$PRUNE"
        ]
    }}
])

当我在mongodb的控制台中使用它时,它可以工作..但是,当我尝试在控制器中调整它时.js

VerifySample: function (req, res) {
    var id = req.body.idSample;
    var idUser=req.body.currentuser;

    SamplePatientModel.aggregate([
        { $match: { _id: id } },
        { $redact: { 
            $cond: [
                { $eq: [ 
                    { $let: {
                        vars: { 
                            "item": { $arrayElemAt: [ "$sampleStatus", -1 ] }
                        },
                        in: "$$item.user"
                    } },
                    idUser
                ] },
                "$$KEEP",
                "$$PRUNE"
            ]
        }}
    ],

    function(err, _SamplePatient) {
          console.log('entry function');

          if (err) {
            console.log('Entry err');
              return res.status(500).json({message: 'Error SamplePatient', error: err});
            }
            //No results
           if(!_SamplePatient){
            console.log('no results ');
            return res.status(404).json({message: 'error', error: err});
           }   

           console.log('Got it');
           console.log(_SamplePatient);


           return res.status(200).json(_SamplePatient);
            }
    );}

它给了我以下回复:

[]

console.log(_SamplePatient)没有显示任何内容

单词"输入功能"在控制台中打印

我做错了什么?

请帮助我。

感谢。

1 个答案:

答案 0 :(得分:1)

聚合管道中不支持在mongoose中投射ObjectId

因此,您必须在聚合管道中将字符串值显式转换为ObjectId。

将您的比赛阶段更新为以下。

{ $match: { _id: mongoose.Types.ObjectId(req.body.idSample) } }

这是问题

https://github.com/Automattic/mongoose/issues/1399

Mongoose Docs:

http://mongoosejs.com/docs/api.html#model_Model.aggregate