2017-04-05 92 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