本文介绍了嵌套数组字段的Mongodb 2dsphere索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了集合,并且一个对象看起来像这样.

I have created collection and a object is looks like this.

[_id] => MongoId Object (
    [$id] => 53087f483b15eaeb6c3c9869
)
[time_from] => 2014-02-22 00:00:00
[time_to] => 2014-02-22 00:10:00
[checkin] => Array (
    [0] => Array (
        [time_frame] => 2014-02-22 00:00:56
        [user_id] => 1
        [loc] => Array (
            [type] => Point
            [coordinates] => Array (
                [0] => 73.43
                [1] => 42.22
            )
        )
    )
    [1] => Array (
        [time_frame] => 2014-02-22 00:00:56
        [user_id] => 2
        [loc] => Array (
            [type] => Point
            [coordinates] => Array (
                [0] => 73.10
                [1] => 42.97
            )
        )
    )
}

我需要为"loc"字段创建"2dsphere"索引.签入字段具有以数组格式存储的所有位置坐标.

I need to create "2dsphere" index for 'loc' field. The checkin field has all the location coordinates stored in array format.

我尝试创建如下所示的索引.

I tried creating index like below.

db.<collection>.ensureIndex( { "checkin.loc" : "2dsphere"  } );

并且我收到以下错误消息.

and I got this below error message.

"err" : "Can't extract geo keys from object, malformed geometry?:{ type: \"Point\", coordinates: [ \"73.10\", \"40.73\" ] }"

我缺少什么?任何帮助,将不胜感激!预先感谢.

What I am missing? Any help would be appreciated! Thanks in advance.

推荐答案

您提供的文档对我来说看起来不错,我还对您的文档的简短版本进行了简单的测试,对我有用.

The document you provided looks good to me, I also did a simple test with a short version of your document and it works for me.

"_id" : ObjectId("530cb07c009d8c323b477957"),
        "time_from" : ISODate("2014-02-25T15:02:20.714Z"),
        "checkin" : [
                {
                        "user_id" : 1,
                        "loc" : {
                                "type" : "Point",
                                "coordinates" : [
                                        73.43,
                                        42.22
                                ]
                        }
                }
        ]

db.testGeo.ensureIndex( { "checkin.loc" : "2dsphere"  } );

因此,我建议检查集合中的其他文档,其中某些文档的索引可能格式错误.还要确保您的坐标数组元素不是字符串.由于此文档不适用于2dsphere索引:

So I suggest checking other documents in the collection, some of them might be malformed for the index. Also make sure that your coordinates array elements are not strings. Because this document is not valid for 2dsphere index:

"_id" : ObjectId("530cb07c009d8c323b477957"),
            "time_from" : ISODate("2014-02-25T15:02:20.714Z"),
            "checkin" : [
                    {
                            "user_id" : 1,
                            "loc" : {
                                    "type" : "Point",
                                    "coordinates" : [
                                            "73.43",
                                            "42.22"
                                    ]
                            }
                    }
            ]

请注意坐标元素的引号使它们成为字符串.

Please note the quotation marks for the coordinates elements which makes them to be strings.

回答评论:Mongo每个集合仅允许一个地理空间索引.因此,您不必为runCommand指定整个字段路径.集合名称就足够了.如果集合名称为checkin_20140222

ANSWER TO THE COMMENT:Mongo allows only one geospatial index per collection. So you don't have to specify the whole field path for your runCommand. Collection name is enough. This should work for you if the collection name is checkin_20140222

db.runCommand( { geoNear: 'checkin_20140222', near: {type: "Point", coordinates: [73.43, 42.22]}, spherical: true, maxDistance: 40000})

希望有帮助!

这篇关于嵌套数组字段的Mongodb 2dsphere索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 13:35