node.js - how to return response using mongo's aggregate? -


i'm starting mongodb, i'm using aggregate function gives me last user of last element samplestatus array. (i mean latest record added samplestatus)

i have collection of samples :

{     "_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 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 use in mongodb's console, works.. but, when try adapt in 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" printed in console

what doing wrong?

please, me.

thanks.

casting objectid in mongoose not supported in aggregation pipeline.

so you've explicitly cast string value objectid in aggregation pipeline.

update match stage below.

{ $match: { _id: mongoose.types.objectid(req.body.idsample) } }

here issue

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

mongoose docs:

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


Comments