如何使用mongo的聚合返回响应? [英] how to return response using mongo's aggregate?

查看:79
本文介绍了如何使用mongo的聚合返回响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从mongodb开始,正在使用聚合函数,该函数使我可以将最后一个元素的最后一个用户带到sampleStatus数组中. (我的意思是将最新记录添加到sampleStatus)

I'm starting with mongodb, I'm using aggregate function which gives me the last user of the last element into the sampleStatus array. (I mean the latest record added to sampleStatus)

我有一个这样的样本集合:

I have a collection of samples like this :

{
    "_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")
        }
    ]
}

这是我正在使用的功能:

This is the function I'm using:

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

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

When I use this in mongodb's console, it works.. but, when I try to adapt this in a 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)没有显示任何内容

控制台上印有进入功能"一词

the words "entry function" are printed in console

我在做什么错了?

请帮帮我.

谢谢.

推荐答案

在聚合管道中不支持在猫鼬中铸造ObjectId.

Casting ObjectId in mongoose is not supported in aggregation pipeline.

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

So you've to explicitly cast the string value to ObjectId in the aggregation pipeline.

将您的比赛阶段更新到下面.

Update your match stage to below.

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

这是问题

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

猫鼬文档:

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

这篇关于如何使用mongo的聚合返回响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆