javascript - Find a maximum value in a collection in nodeJS and MongoDb -
i have simple query cant working. have collection named users has uid field. want maximum value of uid field it. following :-
var options = { "sort": [['uid',-1]] }; db.users.findone({}, options , function(err, doc) { console.log("returned #" + doc.uid + " documents"); });
but gives me 99 while have values upto 300. guess matching every element 9 , comparing it. correct answer should 314
following way entries saved in collection :-
{ { _id: "531181a3ec9af9107d2b4ccd", age: "0", carmodel: "bullet motorcycle", chargeprice: "", contact: "", email: "", fbid: "1179803227", fromlat: "28.4594965", fromlon: "77.0266383", fromname: "gurgaon, haryana, india", fueltype: "petrol", id: "215", image: "http://graph.facebook.com/1179803227/picture?type=large", name: "sandeep yadav", points: "0", regid: "abc", returntime: "15:41 hrs", sex: "0", smoking: "no smoking allowed", starttime: "15:41 hrs", tolat: "28.510782", tolon: "77.048646", toname: "sector 23 ,huda market, huda, sector 23, gurgaon, haryana, india", uid: 311 }, { _id: "531181a3ec9af9107d2b4cce", age: "0", carmodel: "2014 nissan sentra", chargeprice: "usd 30", contact: "", email: "", fbid: "100001451156666", fromlat: "27.950575", fromlon: "-82.4571776", fromname: "tampa, fl, united states", fueltype: "petrol", id: "214", image: "http://graph.facebook.com/100001451156666/picture?type=large", name: "kelly j. dugan", points: "0", regid: "abc", returntime: "23:04 hrs", sex: "0", smoking: "doesnt matter", starttime: "16:02 hrs", tolat: "25.7889689", tolon: "-80.2264393", toname: "miami, fl, united states", uid: 308 } }
try this:
db.users.find({}).sort("uid":-1).limit(1)
that find users, sort them, , return first one.
Comments
Post a Comment