3

Few days ago i was working with MongoDB and aggregation function, but i cant get results that i'm looking.

Suppose a collection that has a document like this:

[_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
    )
)
)

Initially, for the search, i have: the colection, _id value, and url value to search on.

The intention, is for a specific url, example: url: '786527618a424234be70dfb61366', to get the value of the "data" associated with that url, without load all document content. Get only:

 [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
    )

I have been using some forms but havent success results. For example:

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
}

1 Answer 1

1

Finally, i've found a solution:

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.