mongoDB 聚合 : find values on multidimensional array

标签 mongodb aggregation-framework

几天前,我在使用 MongoDB 和聚合函数,但无法获得我正在寻找的结果。

假设一个集合有这样一个文档:

[_id] => 2Q4YkrDUPIdMpHYdG7e801
[domain] => notedlinks.loc
[updateDate] => 1353582907
[pageCaches] => Array (
[0] => Array (
    [url] => 421341234213470dfb61366
    [data] => Array (
        [domain] => notedlinks.loc
        [url] => http://notedlinks.loc/sample/node
        [contentHash] => 382a250d4c226bb85b910c04d1774bb7a9020e44
        [percent] => 100
        [info] => Array (
            [results] => Array (
                [0] => Array (
                    [tag] => Twitter
                    [url] => http://dbpedia.org/resource/Twitter
                    [references] => Array (
                        [0] => twitter
                    )
                )
            )
        )
        [updateDate] => 1353582907
    )
)
[1] => Array (
    [url] => 786527618a424234be70dfb61366
    [data] => Array (
        [domain] => notedlinks.loc
        [url] => http://notedlinks.loc/sample/node
        [contentHash] => 382a250d4c226bb85b910c04d1774bb7a9020e44
        [percent] => 100
        [info] => Array (
            [results] => Array (
                [0] => Array (
                    [tag] => Twitter
                    [url] => http://dbpedia.org/resource/Twitter
                    [references] => Array (
                        [0] => twitter
                    )
                )
            )
        )
        [updateDate] => 1353582907
    )
)
)

最初,对于搜索,我有:要搜索的集合、_id 值和 url 值。

目的是针对特定的 url,例如:url: '786527618a424234be70dfb61366',获取与该 url 关联的“数据”的值,而不加载所有文档内容。仅获取:

 [data] => Array (
        [domain] => notedlinks.loc
        [url] => http://notedlinks.loc/sample/node
        [contentHash] => 382a250d4c226bb85b910c04d1774bb7a9020e44
        [percent] => 100
        [info] => Array (
            [results] => Array (
                [0] => Array (
                    [tag] => Twitter
                    [url] => http://dbpedia.org/resource/Twitter
                    [references] => Array (
                        [0] => twitter
                    )
                )
            )
        )
        [updateDate] => 1353582907
    )

我一直在使用一些表格,但没有成功的结果。 例如:

db.dm_2Q.aggregate({ $match: { _id : "2Q4YkrDUPIdMpHYdG7e801"}, $unwind : "$pageCaches", $project : {pageCaches: 1}, $match : {"pageCaches.url" : "786527618a424234be70dfb61366"}});

{
"result" : [
    {
        "_id" : "2Q4YkrDUPIdMpHYdG7e801",
        "pageCaches" : [
            {
                "url" : "786527618a42084367ccbe70dfb61366",
                "data" : {
                    "domain" : "notedlinks.loc",
                    "url" : "http://notedlinks.loc/sample/node",
                    "contentHash" : "382a250d4c226bb85b910c04d1774bb7a9020e44",
                    "percent" : "100",
                    "info" : {
                        "results" : [
                            {
                                "tag" : "Twitter",
                                "url" : "http://dbpedia.org/resource/Twitter",
                                "references" : [
                                    "twitter"
                                ]
                            }
                        ]
                    },
                    "updateDate" : 1353582907
                }
            },
            {
                "url" : "786527618a424234be70dfb61366",
                "data" : {
                    "domain" : "notedlinks.loc",
                    "url" : "http://notedlinks.loc/sample/node",
                    "contentHash" : "382a250d4c226bb85b910c04d1774bb7a9020e44",
                    "percent" : "100",
                    "info" : {
                        "results" : [
                            {
                                "tag" : "Twitter",
                                "url" : "http://dbpedia.org/resource/Twitter",
                                "references" : [
                                    "twitter"
                                ]
                            }
                        ]
                    },
                    "updateDate" : 1353582907
                }
            }
        ]
    }
],
"ok" : 1
}

最佳答案

最后,我找到了解决方案:

db.dm_2Q.aggregate({$unwind: "$pageCaches"},
{$match:{ "pageCaches.url": "786527618a424234be70dfb61366"}}, 
{$project : {"pageCaches.data": 1}});

{
"result" : [
    {
        "_id" : "2Q4YkrDUPIdMpHYdG7e801",
        "pageCaches" : {
            "data" : {
                "domain" : "notedlinks.loc",
                "url" : "http://notedlinks.loc/sample/node",
                "contentHash" : "382a250d4c226bb85b910c04d1774bb7a9020e44",
                "percent" : "100",
                "info" : {
                    "results" : [
                        {
                            "tag" : "Twitter",
                            "url" : "http://dbpedia.org/resource/Twitter",
                            "references" : [
                                "twitter"
                            ]
                        }
                    ]
                },
                "updateDate" : 1353582907
            }
        }
    }
],
"ok" : 1
}

关于mongoDB 聚合 : find values on multidimensional array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13525862/

相关文章:

node.js - MongoDB聚合: any value of $exists

mongodb 聚合在 golang 中给出错误

mongodb - 匹配数组中的多个值

mongodb - 使用 ReadPreference=NEAREST 的 Mongos 路由

java - DDD Java with Spring - Repository 返回 Mono/Flux

javascript - MongoDB 检查 $switch 语句中的空字段

mongodb - 如何按数组的第一个元素分组?

mongodb - 在 mongoDb 中过滤查询嵌入式文档数组(3 级)

mongodb - 什么会导致mongodb中的段错误

javascript - promise -mongo : can't finalize promise