If I perform the following query in MongoDB shell, all is fine:
db.users.find({location: {$near: [52.3, 6.6], $maxDistance: 0.27}})
However, if I switch the order of arguments, the query fails:
db.users.find({location: {$maxDistance: 0.27, $near: [52.3, 6.6]}})
Mongo returns the following error:
error: {
"$err" : "geo values have to be numbers: { $maxDistance: 0.27, $near: [ 52.3, 6.6 ] }",
"code" : 13026
}
Apparently, the order of the query dictionary’s arguments are important, which strikes me as odd. Can anybody shed some light on this? I’m running MongoDB 2.0.2.
The required order poses a problem for me as I build up the dictionary dynamically and (want to) have no control over the order in which the keys are serialised (which, happens to be the one posing the error).
Well, apparently, Mongo expects ordered dictionary (so called SONs) for some commands, like the
$near-query.In Python, this is done by casting the dictionary to a SON, like so: