2012-11-23 46 views
3

几天前我正在使用MongoDB和聚合函数,但我不能得到我期待的结果。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:'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 
    ) 

我一直在使用某些形式,但没有取得成功的结果。 例如:

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

最后,我已经找到了解决办法:

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 
}