MongoDB .limit() ignoring .sort()? -
on mongodb 2.6.5 using mongo shell run queries
the problem: .limit() seems ignore .sort().
is regular behavior? don't think it's suppose i'm not sure. if not, there way make work sorting , limiting rather limiting sorting.
i'm running following query
db.post.find({categories: {$in: ["101"]}, location: {$near: [1.310000, 103.700000], $maxdistance: 0.449964}, dateexpire: {$gte: isodate("2014-11-27t00:00:00z")}, defunctind: null}, {_id: 1, datecreated: 1}).sort({datecreated: -1}).limit(5);
and result (the dates not in order of sorted list below)
{ "_id" : objectid("5473df733a0da831248b4567"), "datecreated" : isodate("2014-11-25t01:46:26.059z")}
{ "_id" : objectid("546ea9f63a0da8725b8b4725"), "datecreated" : isodate("2014-11-21t02:56:51.143z")}
{ "_id" : objectid("546da7503a0da856058b4633"), "datecreated" : isodate("2014-11-20t08:33:18.504z")}
{ "_id" : objectid("546da6943a0da8725b8b469a"), "datecreated" : isodate("2014-11-20t08:30:10.779z")}
{ "_id" : objectid("546da5c53a0da8725b8b4667"), "datecreated" : isodate("2014-11-20t08:26:42.299z")}
when run above query without limit
db.post.find({categories: {$in: ["101"]}, location: {$near: [1.310000, 103.700000], $maxdistance: 0.449964}, dateexpire: {$gte: isodate("2014-11-27t00:00:00z")}, defunctind: null}, {_id: 1, datecreated: 1}).sort({datecreated: -1})
this result
{ "_id" : objectid("5476accd3a0da8681d8b4596"), "datecreated" : isodate("2014-11-27t04:47:07.078z")}
{ "_id" : objectid("5476ac783a0da8a91d8b4577"), "datecreated" : isodate("2014-11-27t04:45:41.940z")}
{ "_id" : objectid("5476aaba3a0da8681b8b45b7"), "datecreated" : isodate("2014-11-27t04:38:15.751z")}
{ "_id" : objectid("5476a9e63a0da8751d8b4567"), "datecreated" : isodate("2014-11-27t04:34:42.835z")}
{ "_id" : objectid("5473df733a0da831248b4567"), "datecreated" : isodate("2014-11-25t01:46:26.059z")}
{ "_id" : objectid("5472f78f3a0da808608b47b8"), "datecreated" : isodate("2014-11-24t09:17:01.723z")}
{ "_id" : objectid("5472cc0c3a0da8725b8b4765"), "datecreated" : isodate("2014-11-24t06:11:22.772z")}
{ "_id" : objectid("547278bb3a0da8705b8b47b2"), "datecreated" : isodate("2014-11-24t00:15:53.478z")}
{ "_id" : objectid("547012413a0da8705b8b4780"), "datecreated" : isodate("2014-11-22t04:34:07.992z")}
{ "_id" : objectid("5470110a3a0da8705b8b477f"), "datecreated" : isodate("2014-11-22t04:28:55.778z")}
{ "_id" : objectid("546ea9f63a0da8725b8b4725"), "datecreated" : isodate("2014-11-21t02:56:51.143z")}
{ "_id" : objectid("546def203a0da8565b8b464a"), "datecreated" : isodate("2014-11-20t13:39:43.413z")}
{ "_id" : objectid("546deb693a0da856058b4649"), "datecreated" : isodate("2014-11-20t13:23:50.985z")}
{ "_id" : objectid("546da9cc3a0da856058b4647"), "datecreated" : isodate("2014-11-20t08:43:54.626z")}
{ "_id" : objectid("546da8733a0da808608b46ec"), "datecreated" : isodate("2014-11-20t08:38:09.092z")}
thanks
figured out problem.
$near not work $sort. reason why $sort being ignored when placed $limit unless $limit big enough encompass results. sort $near goes nearest distance.
the solution use $geowithin. http://docs.mongodb.org/manual/reference/operator/query/geowithin/#op._s_geowithin
this works $sort. in case of circle, replacement code location: {$geowithin: {$center: [[1.310000, 103.700000], 0.449964]} }
Comments
Post a Comment