Mongodb 2dsphere index for nested array field

ⅰ亾dé卋堺 提交于 2019-12-06 15:59:57

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"  } );

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.

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})

Hope it helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!