【问题标题】:MongoDB query of array of documents returning all elementsMongoDB查询返回所有元素的文档数组
【发布时间】:2015-11-15 17:35:38
【问题描述】:

我有一个名为“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”(或“yes”)的功能。

示例输出可能是:

{
    "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"
    }
},

... 以此类推,直到返回 "gluten free" : "no" 的所有特征。

我在这里缺少什么?提前致谢。

【问题讨论】:

    标签: json mongodb mongodb-query geojson


    【解决方案1】:

    您忘记在投影中添加 positional $ 运算符,它将查询结果中的 features 数组的内容限制为仅包含与查询文档匹配的第一个元素,如下所示:

    db.cupcakes.find(
        { "features.properties.gluten free": "no" },
        { "features.$": 1, "_id": 0 }
    );
    

    样本输出:

    /* 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"
                }
            }
        ]
    }
    

    要取消 gluten free 等于 no 的所有功能,aggregation framework 中的 $redact 管道运算符应该适合您。这将递归地遍历文档结构,并根据对每个级别的指定条件的评估执行一些操作。 map() 方法将按照预期生成最终的对象数组。这个概念可能有点难以理解,但以下示例说明了这一点:

    db.cupcakes.aggregate([
        { 
            "$match": { "features.properties.gluten free" : "no" }
        },
        { 
            "$redact": {
                "$cond": {
                    "if": {
                        "$eq": [ "$gluten free", "yes" ] 
                    },
                    "then": "$$PRUNE",
                    "else": "$$DESCEND"
                }
            }
        }
    ]).map(function(doc){ return doc.features; });
    

    【讨论】:

    • 嗨@chridam 感谢您的回复,很有见地。我怎样才能让无麸质等于没有的所有功能都回撤?
    • 在这种情况下,您可以使用聚合框架的 $redact 管道。查看更新的答案。
    • 嗨@chridam 谢谢,这是有道理的,遗憾的是它没有被烘烤......无论哪种方式,你给出的答案几乎就在那里,但它仍然返回父对象本身(可以这么说)完成类型和几何。有没有办法消除它?目前只有属性对象被修剪。
    • 不太确定我是否清楚地了解了您的问题,您介意编辑您的问题以包含一个最小的文档示例和确切的预期输出吗?
    • 您好 Chridam,仍然遇到似乎所有功能都可以通过的问题,只是有些功能已被删除。谢谢你的帮助。我正在考虑采用不同的方法来简化此操作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2021-10-02
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多