英文:
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
-
基本上与您的先前问题类似。
-
在分组时,可以根据字段的值进行有条件求和。
-
基于
category
和subCategory
的值创建一个key
字段。 -
根据
providedBy
的值,有条件地将其添加到providedByDoctor
或providedByNurse
中。 -
将其分组成单个的
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" }
])
英文:
somewhat similar to your previous question.
you can conditionally sum based on the value of the field when grouping
- Create a
key
field based oncategory
andsubCategory
values - Group by the key and add to
providedByDoctor
orprovidedByNure
conditionally based on the value ofprovidedBy
- 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" }
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论