【问题标题】:MongoDB find internal arrayMongoDB找到内部数组
【发布时间】:2017-08-14 09:25:08
【问题描述】:

我是 MongoDB 的新手,我想过滤一个内部数组。当前文档如下所示:

{
    "data1": "value1",
    "data2": "value2",
    ...
    "array1": [
        {
            "field1": field1value"
            "field2": field2value"
        },
        {
            "field1": expectedvalue"
            "field2": field2value"
        },
    ]
}

我希望得到如下结果:

{
    "data1": "value1",
    "data2": "value2",
    ...
    "array1": [
        {
            "field1": expectedvalue"
            "field2": field2value"
        },
    ]
}

我尝试按照In MongoDB, how do I search in an array of sub-documents? 和其他类似问题中的建议进行查找和汇总。

问题是我丢失了数组之外的所有信息(data1,data2,...):

{
    "_id" : { "$oid" : "8001212651b8a68278edbc92"},
    "array1": [
        {
            "field1": expectedvalue"
            "field2": field2value"
        },
    ]
}

【问题讨论】:

    标签: arrays mongodb filter


    【解决方案1】:

    要仅返回 array1 数组中与给定条件匹配的子文档,您必须使用 projectfilter(在 >= 3.2 版本中可用)。这是一个例子:

    db.collection.aggregate([
        // this is optional, it is only included here to show you that you _can_ match 
        // the documents before you match the array of sub documents
        {$match: {data1: 'value1'}},
        {$project: {
                data1: 1,
                data2: 1,
                array1: {$filter: {
                    input: '$array1',
                    as: 'a',
                    cond: {$eq: ['$$a.field1', 'expectedvalue']}
                }}
            }}
    ])
    

    这将返回以下内容:

    {
        "_id" : ...,
        "data1" : "value1",
        "data2" : "value2",
        "array1" : [ 
            {
                "field1" : "expectedvalue",
                "field2" : "field2value"
            }
        ]
    }
    

    您链接到的答案与 Mongo Java 驱动程序有关,因此,如果您需要通过 Java 驱动程序运行此查询,这里是:

        List<Document> documents = collection.aggregate(Arrays.asList(
                new Document("$project", new Document()
                        // include data1
                        .append("data1", 1)
                        // include data2
                        .append("data2", 1)
                        // include only those elements of array1 which match the filter condition
                        .append("array1", new Document("$filter",
                                new Document("input", "$array1")
                                        .append("as", "a")
                                        .append("cond", new Document("$eq", Arrays.asList("$$a.field1", "expectedvalue"))))))
        )).into(new ArrayList<>());
    

    【讨论】:

    • 只是一个问题:我可以在结果中获得 data1、data2、... 的值,而无需在结果文档中明确地获取/设置它的值吗?
    • 为了过滤子文档数组,您必须使用 $project 运算符,并且由于您使用的是 $project 运算符,因此您必须告诉 MongoDB 您要投影什么,即您必须明确包括data1data2 等,通过向$project 文档添加条目,如下所示:attribute_name: 1
    • 好的,非常感谢。我想我可以做一些类似“用我新过滤的数组替换我的旧数组”的事情,而无需再次构建所有原始*文档*数组外部的数据保持不变。
    猜你喜欢
    • 2013-03-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 2021-05-04
    相关资源
    最近更新 更多