在MongoDB中对对象数组进行分组并获取计数。

huangapple go评论72阅读模式
英文:

group array of objects and get the count in mongodb

问题

我有一个对象数组:

[
  {
    medicationDetails: {
      category: 'Asthma',
      subCategory: '',
      providedBy: 'Doctor'
    }
  },
  {
    medicationDetails: {
      category: 'Diabetes',
      subCategory: 'Oral',
      providedBy: 'Nurse'
    }
  },
  {
    medicationDetails: {
      category: 'Asthma',
      subCategory: '',
      providedBy: 'Doctor'
    }
  },
  {
    medicationDetails: {
      category: 'Diabetes',
      subCategory: 'Insulin',
      providedBy: 'Doctor'
    }
  }
]

一些药物可能有子类别,而其他的则没有。

需要根据'category'、'sub-category'和'providedBy'进行分组,并获得如下计数:

[
  {
    medicationDetails: [
      {
        name: 'Asthma',
        providedByDoctorCount: 2,
        providedByNurseCount: 0
      },
      {
        name: 'Oral',
        providedByDoctorCount: 0,
        providedByNurseCount: 1
      },
      {
        name: 'Insulin',
        providedByDoctorCount: 1,
        providedByNurseCount: 0
      }
    ]
  }
]

我尝试了$group和$count,但没有得到预期的结果。
需要一些有价值的帮助。
英文:

I am having an array of objects:

[
  {
    medicationDetails: {
	  category: 'Asthma',
	  subCategory: '',
	  providedBy: 'Doctor'
    }
  },
  {
    medicationDetails: {
	  category: 'Diabetes',
	  subCategory: 'Oral',
	  providedBy: 'Nurse'
    }
  },
  {
    medicationDetails: {
	  category: 'Asthma',
	  subCategory: ''
	  providedBy: 'Doctor'
    }
  },
  {
    medicationDetails: {
	  category: 'Diabetes',
	  subCategory: 'Insulin',
	  providedBy: 'Doctor'
    }
  }
]

Some medication will have sub-category and some doesn't.

Need to group based on 'category', 'sub-category' and 'providedBy' and get the count like this:

[
  {
    medicationDetails: [
     {
      name: 'Asthma',
	  providedByDoctorCount: 2,
	  providedByNurseCount: 0
     },
     {
      name: 'Oral',
      providedByDoctorCount: 0,
	  providedByNurseCount: 1
     },
     {
      name: 'Insulin',
      providedByDoctorCount: 1,
	  providedByNurseCount: 0
     }
    ]
  }
]

I was tried with $group and $count but not getting the expected result.
Need some valuable help.

答案1

得分: 1

  1. 基本上与您的先前问题类似。

  2. 在分组时,可以根据字段的值进行有条件求和。

  3. 基于 categorysubCategory 的值创建一个 key 字段。

  4. 根据 providedBy 的值,有条件地将其添加到 providedByDoctorprovidedByNurse 中。

  5. 将其分组成单个的 medicationDetails 数组。

db.collection.aggregate([
  { 
   $project: {
      providedBy: "$medicationDetails.providedBy",
      key: { $cond: [ { $eq: [ "$medicationDetails.subCategory", "" ] }, "$medicationDetails.category", "$medicationDetails.subCategory" ] }
    } 
  },
  {
    $group: { 
      _id: "$key",
      providedByDoctor: { $sum: { $cond: [ { $eq: [ "$providedBy", "Doctor" ] }, 1, 0 ] } },
      providedByNurse: { $sum: { $cond: [ { $eq: [ "$providedBy", "Nurse" ] }, 1, 0 ] } } }
  },
  { $group: { _id: null, medicationDetails: { $push: { name: "$_id", providedByDoctorCount: "$providedByDoctor", providedByNurseCount: "$providedByNurse" } } } },
  { $unset: "_id" }
])

playground

英文:

somewhat similar to your previous question.
you can conditionally sum based on the value of the field when grouping

  1. Create a key field based on category and subCategory values
  2. Group by the key and add to providedByDoctor or providedByNure conditionally based on the value of providedBy
  3. Group into a single medicationDetails array
db.collection.aggregate([
  { 
   $project: {
      providedBy: "$medicationDetails.providedBy",
      key: { $cond: [ { $eq: [ "$medicationDetails.subCategory", "" ] }, "$medicationDetails.category", "$medicationDetails.subCategory" ] }
    } 
  },
  {
    $group: { 
      _id: "$key",
      providedByDoctor: { $sum: { $cond: [ { $eq: [ "$providedBy", "Doctor" ] }, 1, 0 ] } },
      providedByNurse: { $sum: { $cond: [ { $eq: [ "$providedBy", "Nurse" ] }, 1, 0 ] } } }
  },
  { $group: { _id: null, medicationDetails: { $push: { name: "$_id", providedByDoctorCount: "$providedByDoctor", providedByNurseCount: "$providedByNurse" } } } },
  { $unset: "_id" }
])

playground

huangapple
  • 本文由 发表于 2023年6月4日 23:17:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定