是否可以将 Mongo GeoNear 命令(使用 Ruby/Rails)返回的数组转换为 Plucky 对象?

发布于 2024-12-20 12:19:57 字数 1014 浏览 2 评论 0原文

作为一个新手,我一直在尝试让 geoNear 命令在我的 Rails 应用程序中工作,它似乎工作正常。对我来说,主要的烦恼是它返回一个带有字符串的数组,而不是我可以调用它来提取数据的键。

经过一番研究,我了解到 MongoMapper 使用 Plucky 将查询结果转换为可以轻松处理的友好对象,但我一直无法找到如何将 geoNear 查询的结果转换为 Plucky 对象。

我的问题是: (a) 是否有可能将其变成一个勇敢的物体?我该怎么做? (b) 如果不可能,我怎样才能最简单、系统地提取每条记录和每一个字段?

这是我的控制器中的查询

@mult = 3963 * (3.14159265 / 180 ) # Scale to miles on earth
@results = @db.command( {'geoNear' => "places", 'near'=> @search.coordinates , 'distanceMultiplier' => @mult, 'spherical' => true})

这是我要返回的对象(为了简单起见,删除了文档内容)

{"ns"=>"myapp-development.places", "near"=>"1001110101110101100100110001100010100010000010111010", "results"=>[{"dis"=>0.04356444023196527, "obj"=>{"_id"=>BSON::ObjectId('4ee6a7d210a81f05fe000001'),...}}], "stats"=>{"time"=>0, "btreelocs"=>0, "nscanned"=>1, "objectsLoaded"=>1, "avgDistance"=>0.04356444023196527, "maxDistance"=>0.0006301239824196907}, "ok"=>1.0}

非常感谢帮助!

As a total newbie I have been trying to get the geoNear command working in my rails application and it appear to be working fine. The major annoyance for me is that it is returning an array with strings rather than keys which I can call on to pull out data.

Having dug around, I understand that MongoMapper uses Plucky to turn the the query resultant into a friendly object which can be handled easily but I haven't been able to find out how to transform the result of my geoNear query into a plucky object.

My questions are:
(a) Is it possible to turn this into a plucky object and how do i do that?
(b) If it is not possible how can I most simply and systematically extract each record and each field?

here is the query in my controller

@mult = 3963 * (3.14159265 / 180 ) # Scale to miles on earth
@results = @db.command( {'geoNear' => "places", 'near'=> @search.coordinates , 'distanceMultiplier' => @mult, 'spherical' => true})

Here is the object i'm getting back (with document content removed for simplicity)

{"ns"=>"myapp-development.places", "near"=>"1001110101110101100100110001100010100010000010111010", "results"=>[{"dis"=>0.04356444023196527, "obj"=>{"_id"=>BSON::ObjectId('4ee6a7d210a81f05fe000001'),...}}], "stats"=>{"time"=>0, "btreelocs"=>0, "nscanned"=>1, "objectsLoaded"=>1, "avgDistance"=>0.04356444023196527, "maxDistance"=>0.0006301239824196907}, "ok"=>1.0}

Help is much appreciated!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许仙没带伞 2024-12-27 12:19:57

好的,假设您将结果存储到名为places_near 的变量中:

places_near = t.command( {'geoNear' => "places", 'near'=> [50,50] , 'distanceMultiplier' = > 1, 'spherical' => true})

此命令返回一个具有映射到列表的键(结果)的哈希值查询的结果。返回的文档如下所示:

{
    "ns": "test.places",
    "near": "1100110000001111110000001111110000001111110000001111",
    "results": [
    {
        "dis": 69.29646421910687,
        "obj": {
            "_id": ObjectId("4b8bd6b93b83c574d8760280"),
            "y": [
            1,
            1
            ],
            "category": "Coffee"
        }
    },
    {
        "dis": 69.29646421910687,
        "obj": {
            "_id": ObjectId("4b8bd6b03b83c574d876027f"),
            "y": [
            1,
            1
            ]
        }
    }
    ],
    "stats": {
        "time": 0,
        "btreelocs": 1,
        "btreelocs": 1,
        "nscanned": 2,
        "nscanned": 2,
        "objectsLoaded": 2,
        "objectsLoaded": 2,
        "avgDistance": 69.29646421910687
    },
    "ok": 1
}

要迭代响应,只需像迭代 ruby​​ 中的任何列表一样迭代即可:

places_near['results'].each do |result|
  # do stuff with result object
end

Ok so lets say you store the results into a variable called places_near:

places_near = t.command( {'geoNear' => "places", 'near'=> [50,50] , 'distanceMultiplier' => 1, 'spherical' => true})

This command returns an hash that has a key (results) which maps to a list of results for the query. The returned document looks like this:

{
    "ns": "test.places",
    "near": "1100110000001111110000001111110000001111110000001111",
    "results": [
    {
        "dis": 69.29646421910687,
        "obj": {
            "_id": ObjectId("4b8bd6b93b83c574d8760280"),
            "y": [
            1,
            1
            ],
            "category": "Coffee"
        }
    },
    {
        "dis": 69.29646421910687,
        "obj": {
            "_id": ObjectId("4b8bd6b03b83c574d876027f"),
            "y": [
            1,
            1
            ]
        }
    }
    ],
    "stats": {
        "time": 0,
        "btreelocs": 1,
        "btreelocs": 1,
        "nscanned": 2,
        "nscanned": 2,
        "objectsLoaded": 2,
        "objectsLoaded": 2,
        "avgDistance": 69.29646421910687
    },
    "ok": 1
}

To iterate over the responses just iterate as you would over any list in ruby:

places_near['results'].each do |result|
  # do stuff with result object
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文