我有一个名为' cupcakes'使用 mongoimport 从GeoJSON文件加载的文件。可以找到GeoJSON here,它是已知的有效JSON文件。这部分似乎已经计划好了,并且正在做:
use cupcakes
db.cupcakes.find({ })
这会按预期拉回所有功能。但是,我正在尝试执行一个查询,该查询只返回将麸质自由设置为" no"。
的功能。我已根据查询arrays of documents的文档尝试了说明。
db.cupcakes.find({features: { $elemMatch: { "properties.gluten free":"no"}}}).pretty()
但据我所知,这似乎是返回所有的功能,而只是那些无麸质的机构。
基本上我想要撤回所有的功能,其中包括免费的" properties.gluten free"功能设置为" no" (或"是"就此而言)。
示例输出可能是:
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-122.70069837570189,
45.53570881624427
]
},
"properties": {
"name": "Le Cookie Monkey",
"address": "1902 NW 24th Avenue",
"website": "http://www.lecookiemonkey.com/",
"gluten free": "no",
"open1": "Tuesday - Friday, 9am - 3pm",
"open2": "Saturday, 9am - 2pm"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-122.6375323534012,
45.5219957171348
]
},
"properties": {
"name": "Crema Coffee + Bakery",
"address": "2728 SE Ankeny Street",
"website": "http://www.cremabakery.com/",
"gluten free": "no",
"open1": "Monday - Sunday, 7am - 6pm"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-122.60960519313811,
45.472775425296014
]
},
"properties": {
"name": "Mehri's Bakery & Deli",
"address": "6923 SE 52nd Avenue",
"website": "http://www.mehris.com/",
"gluten free": "no",
"open1": "Monday - Friday, 7am - 7pm",
"open2": "Saturday, 8am - 5pm",
"open3": "Sunday, 8am - 2pm"
}
},
...依此类推,直到所有的功能,其中"面筋免费" :"不"归还。
我在这里缺少什么?提前谢谢。
答案 0 :(得分:0)
您忘记在投影中添加 positional $
运算符,该运算符限制查询结果中if
数组的内容仅包含与查询文档匹配的第一个元素如下:
features
示例输出:
db.cupcakes.find(
{ "features.properties.gluten free": "no" },
{ "features.$": 1, "_id": 0 }
);
要退回所有无麸质等于否的功能, $redact
中的 aggregation framework 管道运营商应该适合您在这种情况下。这将以递归方式下降到文档结构中,并根据每个级别的指定条件的评估执行一些操作。然后, map()
方法将根据期望生成最终的对象数组。这个概念可能有点棘手,但下面的例子证明了这一点:
/* 0 */
{
"features" : [
{
"type" : "Feature",
"geometry" : {
"type" : "Point",
"coordinates" : [
-122.653357386589,
45.51208367658516
]
},
"properties" : {
"name" : "Hungry Heart Cupcakes",
"address" : "1212 SE Hawthorne Boulevard",
"website" : "http://www.hungryheartcupcakes.com",
"gluten free" : "no",
"open1" : "Monday - Sunday, 11am - 9pm"
}
}
]
}