Have you considered using one of the following approaches?
Find the MAX timeStampField in the collection:
db.collectionName.ensureIndex( { "timeStampField": -1 } ); db.collectionName.find({},{"timeStampField":1}).sort({timeStampField:-1}).limit(1);
OR
Find MAX timeStampField , for user_id in the collection (if the DB is not plastered):
db.collectionName.group( { key: { "user_id" : 1 } , initial: { maxTimeStamp : new Timestamp() } , cond: { timeStampField : {$exists : true}, timeStampField : {$gt: new Timestamp()} } , reduce: function(doc,out) { if (out.maxTimeStamp < doc.timeStampField) { out.maxTimeStamp = doc.timeStampField; } } } );
source share