In my project I need to find the most recent document in collection. I created one but it dose not return anything and I don't where is the problem exactly. Can you help me? Function:
DBManagerConnection.prototype.findDeviceLastDeviceActivity = function(id, callback){
database.DeviceActivity.find({deviceId:id}).sort({deviceLogin:-1}).limit(1), function(err, deviceid){
if(err || !deviceid){
console.log(err);
callback(err, null);
}else{
console.log("Find: " + deviceid);
callback(null, deviceid);
}
}
}
Update: Yup Yup I solved the problem as show bellow:
DBManagerConnection.prototype.findDeviceLastDeviceActivity = function(id, callback){
database.DeviceActivity.find({deviceId:id}).sort({deviceLogin:-1}).limit(1).toArray(function(err, deviceid){
if(err || !deviceid){
console.log(err);
callback(err, null);
}else{
deviceid.forEach(function(item){
console.log("Find: ");
console.dir(item);
});
callback(null, deviceid);
}
});
}
I would welcome any suggestions to improve the function.