【问题标题】:Write a mongo query to count the data where similar data in array?编写一个mongo查询来计算数组中相似数据的数据?
【发布时间】:2021-04-30 05:52:40
【问题描述】:

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


{
    "_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",
                    "value" : [ 
                        "true"
                    ]
                }
            ],
            "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)

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

【问题讨论】:

  • 您认为提供的答案中是否有某些内容无法解决您的问题?如果是这样,那么请对答案发表评论,以澄清究竟需要解决哪些尚未解决的问题。如果它确实回答了您提出的问题,请注意Accept your Answers您提出的问题

标签: arrays mongodb object mongodb-query mongo-collection


【解决方案1】:

你可以试试聚合方法aggregate()

方法一:

  • $match countryCode 的条件应该存在并且system.value 应该是email
  • $filter 迭代 telecom 数组的循环并检查两个条件,这将返回预期的元素
  • $size 从上面的过滤结果中获取总元素
  • $group by null 和 count 总数
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 },
          "system.value": "email"
        }
      }
    }
  },
  { $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

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

【讨论】:

    猜你喜欢
    • 2019-11-28
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多