MongoDB.limit () ignoring .sort ()?

In MongoDB 2.6.5 using mongo shell to run queries

Problem: .limit () seems to ignore .sort ().

Is this a regular behavior? I don't think this is the case, but I'm not sure. If not, is there a way to make it work by sorting and then restricting rather than restricting the sort.

I am making the following request

db.post.find ({category: {$ 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 (dates are not in the 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 with no limit

db.post.find ({category: {$ 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"), "ISO dateCreated" "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 (" 11-24T06 : 22.772Z ")}
{"_id": ObjectId ("547278bb3a0da8705b8b47b2"), "dateCreated": ISODate ("2014-11-24T00: 15: 53.478Z")}
{"_id": ObjectId ("547012413a0da8705b8b4780"), "dateDateCreated": 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")}

thank

+3


source to share


1 answer


Figured out my problem.

$ near doesn't work with $ sort. So the reason why $ sort was always ignored when I put $ limit was if the $ limit was big enough to cover all the results. It sorts by $, near which the nearest distance is.



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 is the place to replace the code: {$ geoWithin: {$ center: [[1.310000, 103.700000], 0.449964]}}

+4


source







All Articles