Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails MongoDB和Ruby:runCommand geoNear和按日期排序_Ruby On Rails_Ruby_Ruby On Rails 3_Mongodb_Mongomapper - Fatal编程技术网

Ruby on rails MongoDB和Ruby:runCommand geoNear和按日期排序

Ruby on rails MongoDB和Ruby:runCommand geoNear和按日期排序,ruby-on-rails,ruby,ruby-on-rails-3,mongodb,mongomapper,Ruby On Rails,Ruby,Ruby On Rails 3,Mongodb,Mongomapper,因此,我想在mongodb上使用runCommand执行geoNear,并希望结果按日期排序 我知道如何做第一部分 db.users.runCommand({'geoNear':'users','near':[-76.483999,42.402794],'spherical':true,'maxDistance':20/6378}) 但是我如何得到结果,使其按创建的顺序排列呢?如果我使用MongoGem来实现这一点,查询将如下所示 User.database.command({'geoNear'

因此,我想在mongodb上使用runCommand执行geoNear,并希望结果按日期排序

我知道如何做第一部分

db.users.runCommand({'geoNear':'users','near':[-76.483999,42.402794],'spherical':true,'maxDistance':20/6378})

但是我如何得到结果,使其按创建的顺序排列呢?如果我使用MongoGem来实现这一点,查询将如下所示

User.database.command({'geoNear'=>users','near'=>[-122,37]},'sphereal'=>true,'maxDistance'=>20/6378)


不过,我不知道如何按日期排序。在本例中,我正在考虑在创建的上使用索引。我在location和created_上都有索引,但结果仍然没有按created_在日期的顺序返回。有人知道怎么做吗

根据链接,我认为不可能在命令中添加排序

有效选项有:“近”、“数”、“最大距离”、“距离乘数” 和“查询”

默认情况下,它按距离排序

或者,您可以这样编写球形查询

db.users.find( { loc :  { $nearSphere : [80.21223299999997, 13.034892],$maxDistance:20/6378  } } ).sort({ created_at : -1 } ) //-1 for descending
您的ruby mongomapper等价物可能是(不确定,最好验证)

Users.where(:loc => {'$nearSphere' => [-122, 37],'$maxDistance'=>20/6378 }).sort(:created_at.desc)