我有一个像这样结构的mongodb集合:
/* 1 */
{
"_id" : ObjectId("551c53f3ecba12e015000045"),
"nome" : "istituzione1",
"username" : "username1",
"email" : "some@email.it",
"pwd" : "189bbbb00c5f1fb7fba9ad9285f193d1",
"punti" : [{
"punto_id" : ObjectId("551c5415ecba12e015000046"),
"nome" : "ORACLE",
"loc" : [-122.262168, 37.531595],
"icona" : 2,
"youtubelink" : "",
"immagini" : [{
"imgid" : ObjectId("551c5d3eecba12e015000084"),
"contenttype" : "image/jpeg"
}, {
"imgid" : ObjectId("551c5d96ecba12e01500008a"),
"contenttype" : "image/jpeg"
}]
}, {
"punto_id" : ObjectId("551c5420ecba12e015000047"),
"nome" : "GOOGLE\r\n",
"loc" : [-122.083983, 37.422969],
"icona" : 2,
"youtubelink" : "",
"immagini" : []
}, {
"punto_id" : ObjectId("551c5d74ecba12e015000089"),
"nome" : "YAHOO",
"loc" : [-122.025061, 37.428061],
"icona" : 1,
"youtubelink" : "",
"immagini" : [{
"imgid" : ObjectId("551c5da4ecba12e01500008e"),
"contenttype" : "image/jpeg"
}, {
"imgid" : ObjectId("551c5daaecba12e015000092"),
"contenttype" : "image/jpeg"
}, {
"name" : "Penguins.jpg",
"imgid" : ObjectId("551c5dfeecba12e015000096"),
"contenttype" : "image/jpeg"
}]
}]
}
我在" loc"上创建了一个2d索引。里面的阵列" punti"我正在尝试这样的地理空间查询:
db.istituzioni.find({ "punti.loc" : { $geoWithin : { $centerSphere : [ [ -121.931076, 37.364700 ], 14.5 / 6371 ] } } })
此查询应仅返回" punti"的元素。与田野" nome"设置为" YAHOO" (我验证了这只创建了另一个集合仅包含" loc"和#34; nome"字段),但它返回整个集合。
我尝试了不同的km值,发现值> = 10.9 km时返回整个集合,而如果值为< 10.9 km查询什么都不返回
我做错了什么?
答案 0 :(得分:1)
查询返回文档,而不是文档中数组的元素。如果您希望查询仅返回punti
的元素,则应更改文档结构或使用punti
元素填充其他集合。
您还可以在投影中使用$ positional运算符来返回punti
数组,只返回与查询条件匹配的第一个元素:
db.istituzioni.find(
{ "punti.loc" : { $geoWithin : { $centerSphere : [ [ -121.931076, 37.364700 ], 14.5 / 6371 ] } } },
{ "punti.$" : 1 }
)