编写一个mongo查询来计算数组中相似数据的数据?

问题描述

样本数据:有多个相似的集合:


{
    "_id" : NumberLong(301),"telecom" : [ 
        {
            "countryCode" : {
                "value" : "+1"
            },"extension" : [ 
                {
                    "url" : "primary","value" : [ 
                        "true"
                    ]
                }
            ],"modifiedValue" : {
                "value" : "8887778888"
            },"system" : {
                "value" : "phone"
            },"useCode" : {
                "value" : "Home Phone"
            },"value" : {
                "value" : "8887778888"
            }
        },{
            "extension" : [ 
                {
                    "url" : "primary","modifiedValue" : {
                "value" : "abc@test.com"
            },"system" : {
                "value" : "email"
            },"useCode" : {
                "value" : "work"
            },"value" : {
                "value" : "abc@test.com"
            }
        }
    ]
}

问题:我想继续电子邮件部分对象中不存在 telecom.system.value = emailcountryCode 的集合。在这里我附上了一个脚本,但我需要一行查询

var count = 0,i;
  db.getCollection('practitioner').find({"telecom.system.value":"email"}).forEach(function(practitioner){
    //print("updating : " +practitioner._id.valueOf())
    telecom = practitioner.telecom.valueOf()
    for(i= 0;i<telecom.length;i++){
        if(telecom[i].system.value === 'email' && telecom[i].countryCode){
        count+=1;
             }
    }
  });
  
    print(" Total count of the practitioner with country code in email object: "+count)

如上所述,脚本运行良好,输出符合我的预期。但脚本没有优化,我想用单行查询编写。提前致谢。

解决方法

您可以尝试聚合方法aggregate()

方法 1:

  • $match 条件 countryCode 应存在且 system.value 应为 email
  • $filter 迭代 telecom 数组的循环并检查两个条件,这将返回预期的元素
  • $size 从上面的过滤结果中获取总元素
  • $group 按 null 和计数总计
var result = await db.getCollection('practitioner').aggregate([
  {
    $match: {
      telecom: {
        $elemMatch: {
          countryCode: { $exists: true },"system.value": "email"
        }
      }
    }
  },{
    $project: {
      count: {
        $size: {
          $filter: {
            input: "$telecom",cond: {
              $and: [
                { $ne: [{ $type: "$$this.countryCode" },"missing"] },{ $eq: ["$$this.system.value","email"] }
              ]
            }
          }
        }
      }
    }
  },{
    $group: {
      _id: null,count: { $sum: "$count" }
    }
  }
]);

print("Total count of the practitioner with country code in email object: "+result[0].count);

Playground


方法 2:

  • $match 条件 countryCode 应存在且 system.value 应为 email
  • $unwind 解构 telecom 数组
  • $match 使用上述条件过滤文档
  • $count 获取元素总数
var result = await db.getCollection('practitioner').aggregate([
  {
    $match: {
      telecom: {
        $elemMatch: {
          countryCode: { $exists: true },{ $unwind: "$telecom" },{
    $match: {
      "telecom.countryCode": { $exists: true },"telecom.system.value": "email"
    }
  },{ $count: "count" }
]);

print("Total count of the practitioner with country code in email object: "+result[0].count);

Playground

我没有测试性能,但您可以根据您的要求检查和使用。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...