您提供的文档对我来说看起来不错,我还使用您文档的简短版本进行了简单的测试,它对我有用。
"_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 索引无效:
"_id" : ObjectId("530cb07c009d8c323b477957"),
"time_from" : ISODate("2014-02-25T15:02:20.714Z"),
"checkin" : [
{
"user_id" : 1,
"loc" : {
"type" : "Point",
"coordinates" : [
"73.43",
"42.22"
]
}
}
]
请注意坐标元素的引号,这使得它们成为字符串。
对评论的答复:Mongo 只允许每个集合有一个地理空间索引。因此,您不必为 runCommand 指定整个字段路径。集合名称就足够了。如果集合名称是,这应该对您有用checkin_20140222
db.runCommand( { geoNear: 'checkin_20140222', near: {type: "Point", coordinates: [73.43, 42.22]}, spherical: true, maxDistance: 40000})
希望能帮助到你!