1

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.

user3492113
  • 13
  • 1
  • 6
  • 4
    I'm glad you got it working. As you answered your own question and now are just asking for opinions, please do one of the following two things: 1) move your Update to an answer and mark it as answered (this will help people searching in the future to see that there's an answer); or 2) delete the question altogether (SO isn't good for opinion-based questions). – Eli Gassert Jun 23 '14 at 15:43

0 Answers0