我读过这些文档,但不像预期的那样工作。我有以下文件:
{
"_id" : ObjectId("5396e85c12f43f5d1bafbd13"),
"author" : ObjectId("5396ca2b0fe95cf96599d881"),
"location" : {
"address" : "An address",
"coordinates" : [
12.52891929999998,
16.620259
],
"type" : "Point",
"postal_code" : "333",
"country" : "Country",
},
"description" : "aDescription"
}我在蒙戈有以下指数:
{
"location" : "2dsphere"
}如何创建复合索引?以下内容正确:
db.articles.ensureIndex( { location : "2dsphere", postal_code:1, country:1, address:1 } )postal_code:1或-1是什么意思?
发布于 2014-06-11 01:45:24
您的文件的正确格式是:
db.articles.ensureIndex({
"location" : "2dsphere",
"location.postal_code":1,
"location.country":1,
"location.address":1
})因为这些字段是"location“子文档的一部分,所以您可以使用”点符号“指定完整的路径。
1或-1的表示法分别代表升序和降序。
请注意,作为一个复合索引,这些字段在组合使用此索引的Geo空间查询时才真正对您有用,因为"2dsphere“索引元素是第一个。我建议,“国家”对于搜索“近”或“内”点的协调有些过于宽泛。但其他人可以有效地过滤结果。
https://stackoverflow.com/questions/24153401
复制相似问题