Angel Pineda
Angel Pineda

Reputation: 177

how to return response using mongo's aggregate?

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

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);
            }
    );}

It gives me following response:

[]

console.log(_SamplePatient) doesn't show anything

the words "entry function" are printed in console

what am I doing wrong?

Please, help me.

Thanks.

Upvotes: 0

Views: 2032

Answers (1)

s7vr
s7vr

Reputation: 75984

Casting ObjectId in mongoose is not supported in aggregation pipeline.

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) } }

Here is the issue

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

Mongoose Docs:

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

Upvotes: 1

Related Questions