mongodb - Geonear sort by distance and time -


i have 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 first of all, sort distance, if distance same, sort 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 sort on both "nearest" , "createddate"?

the correct query use here uses aggregation framework has $geonear pipeline stage assist this. it's place "sort" multiple keys, unforntunately "geospatial" $nearsphere not have "meta" projection "distance" $text has "score".

also geonear database command using can not used "cursor" .sort() in way either.

db.paging.aggregate([     { "$geonear": {         "near": [106.606033,29.575897 ],         "spherical": true,         "distancefield": "distance",         "distancemuliplier": 6371,         "maxdistance": 1/6371     }},     { "$sort": { "distance": 1, "createdate": -1 } },     { "$skip": ( 2-1 ) * 2 },     { "$limit": 5 } ]) 

that equivalent of trying do.

with aggregation framework use "pipeline operators" instead of "cursor modifiers" things $sort, $skip , $limit. these must in logical order, whereas cursor modifiers work out.

it's "pipeline", "unix pipe". |

also, careful "maxdistance" , "distancemuliplier". since co-ordinates in "legacy co-ordinate pairs" , not geojson format, distances measured in "radians". if have geojson stored location data result returned in "meters".


Comments