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

时间:2020-08-28 23:11:21

标签: python mongodb mongodb-query aggregation-framework pymongo

我有一个属性模式(https://www.mongodb.com/blog/post/building-with-patterns-the-attribute-pattern)字段,如下所示:

"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"
        }
      .
      .
      .

我想创建一个聚合管道,以查找仅患有ischemic_hd的所有患者,而所有其他可能的疾病均为0。但是我不确定如何创建此查询?

1 个答案:

答案 0 :(得分:1)

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

如果要排除其他所有内容,请使用@State private var data = UserDefaults.standard.integer(forKey: “DataIndex") var images = [“example”, “example”, …] Picker(selection: $data, label: Text(“Data”)) { ForEach(images, id: \.self) { image in Image(image) .resizable() .scaledToFit() .rotationEffect(Angle(degrees: 90)) } } .labelsHidden() .rotationEffect(Angle(degrees: -90)) .frame(maxHeight: 100) .clipped() 对所有属性的值求和,并在count = 1处进行匹配。

$reduce