【问题标题】:how to return response using mongo's aggregate?如何使用 mongo 的聚合返回响应?
【发布时间】:2017-08-31 11:10:30
【问题描述】:

我从 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 的控制台中使用它时,它可以工作.. 但是,当我尝试在 controller.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) 不显示任何内容

控制台打印“入口函数”字样

我做错了什么?

请帮帮我。

谢谢。

【问题讨论】:

  • 非常感谢@veeram

标签: node.js mongodb mongoose aggregation-framework


【解决方案1】:

聚合管道不支持在 mongoose 中转换 ObjectId

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

将你的比赛阶段更新到下面。

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

问题来了

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

猫鼬文档:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-23
    • 2020-05-02
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 2021-04-22
    相关资源
    最近更新 更多