3

数日前、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
}
4

1 に答える 1

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
}
于 2012-11-23T09:43:20.107 に答える