Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python MongoDB聚合属性模式管道/查询_Python_Mongodb_Mongodb Query_Aggregation Framework_Pymongo - Fatal编程技术网

Python MongoDB聚合属性模式管道/查询

Python MongoDB聚合属性模式管道/查询,python,mongodb,mongodb-query,aggregation-framework,pymongo,Python,Mongodb,Mongodb Query,Aggregation Framework,Pymongo,我的属性模式()字段如下所示: "cmr_diag": [{ "name": "shd?", "value": { "$numberDouble": "1" } }, { "name": "ischemic_hd", "

我的属性模式()字段如下所示:

"cmr_diag": [{
        "name": "shd?",
        "value": {
            "$numberDouble": "1"
        }
    }, {
        "name": "ischemic_hd",
        "value": {
            "$numberDouble": "1"
        }
    }, {
        "name": "non-ischemic_dcmp",
        "value": {
            "$numberDouble": "1"
        }
    }, {
        "name": "myocarditis",
        "value": {
            "$numberDouble": "0"
        }
    }, {
        "name": "hcm",
        "value": {
            "$numberDouble": "0"
        }
    }, {
        "name": "amyloidosis",
        "value": {
            "$numberDouble": "0"
        }
    }, {
        "name": "toxic_cmp",
        "value": {
            "$numberDouble": "1"
        }
      .
      .
      .

我想创建一个聚合管道,用于查找所有仅患有缺血性心脏病的患者,而所有其他可能的疾病都是0。但是,我不知道如何创建此查询?

您可以使用$elemMatch来识别具有特定属性的患者

如果要排除所有其他属性,请使用
$reduce
对所有属性的值求和,并匹配其中的count=1

db.collection.aggregate([
   {$match: {
       cmr_diag: {
           $elemMatch: {
                  name: "ischemic_hd",
                  value: { "$numberDouble": "1" }
           }
       }
   }},
   {$addFields: {
       diagcount: {
          $reduce: {
              input: "$cmr_diag",
              initialValue: 0,
              in: {$sum: ["$$value","$$this.value.$numberDouble"]}
          }
       }
    }},
    {$match: { diagcount: 1}}
])

你能给这篇文章增加一个例子来说明你最终的结果需要什么吗?非常感谢