2017-04-05 94 views
0

匹配的文件,並得到所有值在蒙戈3.4使用MongoDB的聚集到了現場

我與格式文檔的集合:

類型1:

{ 
"Level1": { 
    "@version": "genR", 
    "@revision": "aux", 
    "Level2": { 
     "container": { 
      "type": "ARRAY", 
      "categories": [ 
       { 
        "category": [ 
         { 
          "Type": "STRING", 
          "Value": "Currency" 
         }, 
         { 
          "Type": "STRING", 
          "Value": "EUR" 
         } 
        ] 
       }, 
       { 
        "category": [ 
         { 
          "Type": "STRING", 
          "Value": "Portfolio" 
         }, 
         { 
          "Type": "STRING", 
          "Value": "ABCDEF" 
         } 
        ] 
       }, 
      ] 
      } 
     } 
    } 
} 

類型2:

{ 
"Level1": { 
    "@version": "genR", 
    "@revision": "aux", 
    "Level2": { 
     "container": { 
      "type": "ARRAY", 
      "categories": [ 
       { 
        "category": [ 
         { 
          "Type": "STRING", 
          "Value": "Currency" 
         }, 
         { 
          "Type": "STRING", 
          "Value": "EUR" 
         } 
        ] 
       }, 
       { 
        "category": [ 
         { 
          "Type": "STRING", 
          "Value": "Portfolio" 
         }, 
         { 
          "Type": "STRING", 
          "Value": "ABCDEF" 
         } 
        ] 
       }, 
       { 
        "category": [ 
         { 
          "Type": "STRING", 
          "Value": "Short Description" 
         }, 
         { 
          "Type": "STRING", 
          "Value": "Cash Only" 
         } 
        ] 
       }, 
      ] 
      } 
     } 
    } 
} 

我該如何編寫一個彙總語句,以便我可以從文檔中獲得所有貨幣值在哪裏投資組合匹配一定的價值。

我一直用如下pymongo的總框架:

pipeline = [{"$unwind":"$Level1.Level2.container.categories"},{"$unwind":"$Level1.Level2.container.categories.category"},{"$match":{"Level1.Level2.container.categories.category.Value":"Portfolio"}}] 
pprint(db.command('aggregate',collection,pipeline=pipeline)) 

但沒有結果。 Pymongo有點混亂。即使有人可以指出一般的方法,它確實會有所幫助。

預期的響應假設4個匹配文件(分別設定不同的類別項目數量):

{'Currency': [{'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'EUR'}}}}}}, 
      {'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'EUR'}}}}}}, 
      {'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'USD'}}}}}}, 
      {'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'EUR'}}}}}}]} 

回答

0

您的結構不理想,但你可以用下面的查詢。

的下面$match階段$and的兩個條件。看起來在category陣列($elemMatch)下categories$elemMatch)陣列,用於同時滿足($allPortfolio匹配ABCDEF值條件,然後條件元件相Currrency值的元素。

$unwind階段是打破categories隨後$match保持Currencycategory嵌入式陣列的文件。

$unwind階段是打破category隨後$match除去CurrencyValue嵌入的文檔。

最後兩個階段是$group + $push剩餘數據到嵌入式陣列和$projectCurrency值。

您可以一次查看中間輸出爲更好地瞭解運行一個階段。

db.collection.aggregate( 
    { $match : 
     { $and : 
      [ 
       { "Level1.Level2.container.categories": 
       { $elemMatch: 
        { "category": 
         { $all: 
         [ 
          { $elemMatch : { "Type": "STRING", "Value": "Portfolio" } }, 
          { $elemMatch : { "Type": "STRING", "Value": "ABCDEF" } } 
         ] 
         } 
        } 
        } 
       }, 
       { "Level1.Level2.container.categories": 
        { $elemMatch: 
        { "category": 
         { $elemMatch : { "Type": "STRING", "Value": "Currency" } } 
         } 
        } 
       } 
      ] 
     } 
    }, 
    { $unwind : "$Level1.Level2.container.categories" }, 
    { $match : { "Level1.Level2.container.categories.category.Value": "Currency" } }, 
    { $unwind : "$Level1.Level2.container.categories.category" }, 
    { $match : { "Level1.Level2.container.categories.category.Value": { $ne : "Currency" } } }, 
    { $group: { _id: null, "Currency": { $push: "$$ROOT" } } }, 
    { $project: { _id: 0, "Currency.Level1.Level2.container.categories.category.Value": 1 } }) 
+0

這是給我所有的類別,除了投資組合。但是你指出了我的正確方向。我會盡快更新評論。謝謝 – sunny

+0

Np。請添加預期的json響應,並相應地調整查詢。 – Veeram

+0

我還有其他文檔具有比'Portfolio'和'Currency'更多的'category'字段。此類別子結構基於文檔類型是動態的。 – sunny