I have the following data:
{
"_id" : ObjectId("55a8c1ba3996c909184d7a22"),
"uid" : "1db82e8a-2038-4818-b805-76a46ba62639",
"createdate" : ISODate("2015-07-17T08:50:02.892Z"),
"palce" : "aa",
"sex" : 1,
"longdis" : 1,
"location" : [ 106.607312, 29.575281 ]
}
{
"_id" : ObjectId("55a8c1ba3996c909184d7a24"),
"uid" : "1db82e8a-2038-4818-b805-76a46ba62639",
"createdate" : ISODate("2015-07-17T08:50:02.920Z"),
"palce" : "bbb",
"sex" : 1,
"longdis" : 1,
"location" : [ 106.589896, 29.545098 ]
}
{
"_id" : ObjectId("55a8c1ba3996c909184d7a25"),
"uid" : "1db82e8a-2038-4818-b805-76a46ba62639",
"createdate" : ISODate("2015-07-17T08:50:02.922Z"),
"palce" : "ccc",
"sex" : 1,
"longdis" : 1,
"location" : [ 106.590758, 29.566713 ]
}
{
"_id" : ObjectId("55a8c1ba3996c909184d7a26"),
"uid" : "1db82e8a-2038-4818-b805-76a46ba62639",
"createdate" : ISODate("2015-07-17T08:50:02.923Z"),
"palce" : "ddd",
"sex" : 1,
"longdis" : 1,
"location" : [ 106.637039, 29.561436 ]
}
{
"_id" : ObjectId("55a8c1bc3996c909184d7a27"),
"uid" : "1db82e8a-2038-4818-b805-76a46ba62639",
"createdate" : ISODate("2015-07-17T08:50:04.499Z"),
"palce" : "eee",
"sex" : 1,
"longdis" : 1,
"location" : [ 106.539522, 29.57929 ]
}
{
"_id" : ObjectId("55a8d12e78292fa3837ebae4"),
"uid" : "1db82e8a-2038-4818-b805-76a46ba62639",
"createdate" : ISODate("2015-07-17T09:55:58.947Z"),
"palce" : "fff",
"sex" : 1,
"longdis" : 1,
"location" : [ 106.637039, 29.561436 ]
}
I want to first of all, sort by the distance, if the distance is the same, sort by the time.
my command :
db.runCommand( {
geoNear: "paging",
near: [106.606033,29.575897 ],
spherical : true,
maxDistance : 1/6371,
minDistance:0/6371,
distanceMultiplier: 6371,
num:2,
query: {'_id': {'$nin': []}}
})
or
db.paging.find({
'location':{
$nearSphere: [106.606033,29.575897],
$maxDistance:1
}
}).limit(5).skip((2 - 1) * 2).sort({createdate:-1})
How can I sort on both "nearest" and "createddate"?
The correct query to use here uses the aggregation framework which has the
$geoNear
pipeline stage to assist with this. It's also the only place you get to "sort" by multiple keys, as unforntunately the "geospatial"$nearSphere
does not have a "meta" projection for "distance" like$text
has a "score".Also the
geoNear
database command you are using can also not be used with "cursor".sort()
in that way either.That is the equivalent of what you are trying to do.
With the aggregation framework you use the "pipeline operators" instead of "cursor modifiers" to do things like
$sort
,$skip
and$limit
. Also these must be in a Logical order, whereas the cursor modifiers generally work it out.It's a "pipeline", just like "Unix pipe".
|
Also, be careful with "maxDistance" and "distanceMuliplier". Since your co-ordinates are in "legacy co-ordinate pairs" and not
GeoJSON
format, then the distances are measured in "radians". If you have GeoJSON stored location data then the result is returned in "meters".