Reputation: 141
On MongoDB 2.6.5 using mongo shell to run the queries
The problem: .limit() seems to ignore .sort().
Is that the regular behavior? I don't think it's suppose to do that but I'm not sure. If not, is there a way to make it work by sorting and then limiting rather than limiting then sorting.
I'm running the 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 this is the result (The dates are not in order of the 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 I run the above query without the 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 is the 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
Upvotes: 4
Views: 573
Reputation: 141
Figured out my problem.
$near does not work with $sort. Thus the reason why $sort was always being ignored when I placed $limit unless the $limit was big enough to encompass all the results. It would sort by $near which goes by nearest distance.
The solution is to use $geoWithin. http://docs.mongodb.org/manual/reference/operator/query/geoWithin/#op._S_geoWithin
This works with $sort. In my case of a circle, this was the replacement code location: {$geoWithin: {$center: [[1.310000, 103.700000], 0.449964]} }
Upvotes: 4