How to create an aggregate based on unique elements as fields?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to aggregate documents based on diagnosisType, and at a higher level patientKey
and encounterKey
. This is what the documents look like:
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 2,
"diagnosisType" : "medical_history",
"diagnosisCode" : "Z87.81",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 2,
"diagnosisType" : "problem_list",
"diagnosisCode" : "2x.2",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "medical_history",
"diagnosisCode" : "D21.01",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "medical_history",
"diagnosisCode" : "X2.31",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "problem_list",
"diagnosisCode" : "p.2342",
}
This is what I would like the aggregate to look like:
{
"patientKey": 1,
"encounters":[{
encounterKey: 2,
medical_history: [Z87.81],
problem_list: [2x.2]
},
{
encounterKey: 3,
medical_history: [D21.01, X2.31],
problem_list: [p.2342]
}]
}
Any ideas how I could approach this?
I have tried the following for medical history and then problem list, my initial thought is to first aggregate on Problem List and then on Medical History, then combine the collections, afterwards do another aggregate based on encounters, and then finally another aggregate on patientid.
But it's a problem combining heterogeous documents.
db.collections.aggregate([
{$match:{diagnosisType:"Problem List"}},
{$group:{_id:"$encounterKey",
"ProblemList": {$push:{$concat:"$diagnosisCode"}},
"durableKey":{$first:"$durableKey"}}},
{$project:{_id:0,
encounterKey:"$_id",
ProblemList:1,
durableKey:1}},
{$out:"output"}
],{
allowDiskUse: true
})
mongodb aggregation-framework
add a comment |
I am trying to aggregate documents based on diagnosisType, and at a higher level patientKey
and encounterKey
. This is what the documents look like:
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 2,
"diagnosisType" : "medical_history",
"diagnosisCode" : "Z87.81",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 2,
"diagnosisType" : "problem_list",
"diagnosisCode" : "2x.2",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "medical_history",
"diagnosisCode" : "D21.01",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "medical_history",
"diagnosisCode" : "X2.31",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "problem_list",
"diagnosisCode" : "p.2342",
}
This is what I would like the aggregate to look like:
{
"patientKey": 1,
"encounters":[{
encounterKey: 2,
medical_history: [Z87.81],
problem_list: [2x.2]
},
{
encounterKey: 3,
medical_history: [D21.01, X2.31],
problem_list: [p.2342]
}]
}
Any ideas how I could approach this?
I have tried the following for medical history and then problem list, my initial thought is to first aggregate on Problem List and then on Medical History, then combine the collections, afterwards do another aggregate based on encounters, and then finally another aggregate on patientid.
But it's a problem combining heterogeous documents.
db.collections.aggregate([
{$match:{diagnosisType:"Problem List"}},
{$group:{_id:"$encounterKey",
"ProblemList": {$push:{$concat:"$diagnosisCode"}},
"durableKey":{$first:"$durableKey"}}},
{$project:{_id:0,
encounterKey:"$_id",
ProblemList:1,
durableKey:1}},
{$out:"output"}
],{
allowDiskUse: true
})
mongodb aggregation-framework
add a comment |
I am trying to aggregate documents based on diagnosisType, and at a higher level patientKey
and encounterKey
. This is what the documents look like:
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 2,
"diagnosisType" : "medical_history",
"diagnosisCode" : "Z87.81",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 2,
"diagnosisType" : "problem_list",
"diagnosisCode" : "2x.2",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "medical_history",
"diagnosisCode" : "D21.01",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "medical_history",
"diagnosisCode" : "X2.31",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "problem_list",
"diagnosisCode" : "p.2342",
}
This is what I would like the aggregate to look like:
{
"patientKey": 1,
"encounters":[{
encounterKey: 2,
medical_history: [Z87.81],
problem_list: [2x.2]
},
{
encounterKey: 3,
medical_history: [D21.01, X2.31],
problem_list: [p.2342]
}]
}
Any ideas how I could approach this?
I have tried the following for medical history and then problem list, my initial thought is to first aggregate on Problem List and then on Medical History, then combine the collections, afterwards do another aggregate based on encounters, and then finally another aggregate on patientid.
But it's a problem combining heterogeous documents.
db.collections.aggregate([
{$match:{diagnosisType:"Problem List"}},
{$group:{_id:"$encounterKey",
"ProblemList": {$push:{$concat:"$diagnosisCode"}},
"durableKey":{$first:"$durableKey"}}},
{$project:{_id:0,
encounterKey:"$_id",
ProblemList:1,
durableKey:1}},
{$out:"output"}
],{
allowDiskUse: true
})
mongodb aggregation-framework
I am trying to aggregate documents based on diagnosisType, and at a higher level patientKey
and encounterKey
. This is what the documents look like:
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 2,
"diagnosisType" : "medical_history",
"diagnosisCode" : "Z87.81",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 2,
"diagnosisType" : "problem_list",
"diagnosisCode" : "2x.2",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "medical_history",
"diagnosisCode" : "D21.01",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "medical_history",
"diagnosisCode" : "X2.31",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "problem_list",
"diagnosisCode" : "p.2342",
}
This is what I would like the aggregate to look like:
{
"patientKey": 1,
"encounters":[{
encounterKey: 2,
medical_history: [Z87.81],
problem_list: [2x.2]
},
{
encounterKey: 3,
medical_history: [D21.01, X2.31],
problem_list: [p.2342]
}]
}
Any ideas how I could approach this?
I have tried the following for medical history and then problem list, my initial thought is to first aggregate on Problem List and then on Medical History, then combine the collections, afterwards do another aggregate based on encounters, and then finally another aggregate on patientid.
But it's a problem combining heterogeous documents.
db.collections.aggregate([
{$match:{diagnosisType:"Problem List"}},
{$group:{_id:"$encounterKey",
"ProblemList": {$push:{$concat:"$diagnosisCode"}},
"durableKey":{$first:"$durableKey"}}},
{$project:{_id:0,
encounterKey:"$_id",
ProblemList:1,
durableKey:1}},
{$out:"output"}
],{
allowDiskUse: true
})
mongodb aggregation-framework
mongodb aggregation-framework
edited Jan 4 at 17:20
mickl
16.3k61741
16.3k61741
asked Jan 4 at 16:56
user252046user252046
6318
6318
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use below aggregation:
db.col.aggregate([
{
$group: {
_id: { patientKey: "$patientKey", encounterKey: "$encounterKey", diagnosisType: "$diagnosisType" },
problem_list: { $push: "$diagnosisCode" }
}
},
{
$group: {
_id: { patientKey: "$_id.patientKey", encounterKey: "$_id.encounterKey" },
encounters: { $push: { k: "$_id.diagnosisType", v: "$problem_list" } }
}
},
{
$project: {
encounters: { $arrayToObject: "$encounters" }
}
},
{
$group: {
_id: "$_id.patientKey",
encounters: {
$push: {
encounterKey: "$_id.encounterKey",
problem_list: "$encounters.problem_list",
medical_history: "$encounters.medical_history"
}
}
}
},
{
$project: {
patientKey: "$_id",
_id: 0,
encounters: 1
}
}
])
Basically you need few $group stages to accumulate the data. Additionally you have to use $arrayToObject to build your JSON keys dynamically.
Outputs:
{
"patientKey" : 1,
"encounters" : [
{
"encounterKey" : 2,
"problem_list" : [
"2x.2"
],
"medical_history" : [
"Z87.81"
]
},
{
"encounterKey" : 3,
"problem_list" : [
"p.2342"
],
"medical_history" : [
"D21.01",
"X2.31"
]
}
]
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54043124%2fhow-to-create-an-aggregate-based-on-unique-elements-as-fields%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use below aggregation:
db.col.aggregate([
{
$group: {
_id: { patientKey: "$patientKey", encounterKey: "$encounterKey", diagnosisType: "$diagnosisType" },
problem_list: { $push: "$diagnosisCode" }
}
},
{
$group: {
_id: { patientKey: "$_id.patientKey", encounterKey: "$_id.encounterKey" },
encounters: { $push: { k: "$_id.diagnosisType", v: "$problem_list" } }
}
},
{
$project: {
encounters: { $arrayToObject: "$encounters" }
}
},
{
$group: {
_id: "$_id.patientKey",
encounters: {
$push: {
encounterKey: "$_id.encounterKey",
problem_list: "$encounters.problem_list",
medical_history: "$encounters.medical_history"
}
}
}
},
{
$project: {
patientKey: "$_id",
_id: 0,
encounters: 1
}
}
])
Basically you need few $group stages to accumulate the data. Additionally you have to use $arrayToObject to build your JSON keys dynamically.
Outputs:
{
"patientKey" : 1,
"encounters" : [
{
"encounterKey" : 2,
"problem_list" : [
"2x.2"
],
"medical_history" : [
"Z87.81"
]
},
{
"encounterKey" : 3,
"problem_list" : [
"p.2342"
],
"medical_history" : [
"D21.01",
"X2.31"
]
}
]
}
add a comment |
You can use below aggregation:
db.col.aggregate([
{
$group: {
_id: { patientKey: "$patientKey", encounterKey: "$encounterKey", diagnosisType: "$diagnosisType" },
problem_list: { $push: "$diagnosisCode" }
}
},
{
$group: {
_id: { patientKey: "$_id.patientKey", encounterKey: "$_id.encounterKey" },
encounters: { $push: { k: "$_id.diagnosisType", v: "$problem_list" } }
}
},
{
$project: {
encounters: { $arrayToObject: "$encounters" }
}
},
{
$group: {
_id: "$_id.patientKey",
encounters: {
$push: {
encounterKey: "$_id.encounterKey",
problem_list: "$encounters.problem_list",
medical_history: "$encounters.medical_history"
}
}
}
},
{
$project: {
patientKey: "$_id",
_id: 0,
encounters: 1
}
}
])
Basically you need few $group stages to accumulate the data. Additionally you have to use $arrayToObject to build your JSON keys dynamically.
Outputs:
{
"patientKey" : 1,
"encounters" : [
{
"encounterKey" : 2,
"problem_list" : [
"2x.2"
],
"medical_history" : [
"Z87.81"
]
},
{
"encounterKey" : 3,
"problem_list" : [
"p.2342"
],
"medical_history" : [
"D21.01",
"X2.31"
]
}
]
}
add a comment |
You can use below aggregation:
db.col.aggregate([
{
$group: {
_id: { patientKey: "$patientKey", encounterKey: "$encounterKey", diagnosisType: "$diagnosisType" },
problem_list: { $push: "$diagnosisCode" }
}
},
{
$group: {
_id: { patientKey: "$_id.patientKey", encounterKey: "$_id.encounterKey" },
encounters: { $push: { k: "$_id.diagnosisType", v: "$problem_list" } }
}
},
{
$project: {
encounters: { $arrayToObject: "$encounters" }
}
},
{
$group: {
_id: "$_id.patientKey",
encounters: {
$push: {
encounterKey: "$_id.encounterKey",
problem_list: "$encounters.problem_list",
medical_history: "$encounters.medical_history"
}
}
}
},
{
$project: {
patientKey: "$_id",
_id: 0,
encounters: 1
}
}
])
Basically you need few $group stages to accumulate the data. Additionally you have to use $arrayToObject to build your JSON keys dynamically.
Outputs:
{
"patientKey" : 1,
"encounters" : [
{
"encounterKey" : 2,
"problem_list" : [
"2x.2"
],
"medical_history" : [
"Z87.81"
]
},
{
"encounterKey" : 3,
"problem_list" : [
"p.2342"
],
"medical_history" : [
"D21.01",
"X2.31"
]
}
]
}
You can use below aggregation:
db.col.aggregate([
{
$group: {
_id: { patientKey: "$patientKey", encounterKey: "$encounterKey", diagnosisType: "$diagnosisType" },
problem_list: { $push: "$diagnosisCode" }
}
},
{
$group: {
_id: { patientKey: "$_id.patientKey", encounterKey: "$_id.encounterKey" },
encounters: { $push: { k: "$_id.diagnosisType", v: "$problem_list" } }
}
},
{
$project: {
encounters: { $arrayToObject: "$encounters" }
}
},
{
$group: {
_id: "$_id.patientKey",
encounters: {
$push: {
encounterKey: "$_id.encounterKey",
problem_list: "$encounters.problem_list",
medical_history: "$encounters.medical_history"
}
}
}
},
{
$project: {
patientKey: "$_id",
_id: 0,
encounters: 1
}
}
])
Basically you need few $group stages to accumulate the data. Additionally you have to use $arrayToObject to build your JSON keys dynamically.
Outputs:
{
"patientKey" : 1,
"encounters" : [
{
"encounterKey" : 2,
"problem_list" : [
"2x.2"
],
"medical_history" : [
"Z87.81"
]
},
{
"encounterKey" : 3,
"problem_list" : [
"p.2342"
],
"medical_history" : [
"D21.01",
"X2.31"
]
}
]
}
answered Jan 4 at 17:14
micklmickl
16.3k61741
16.3k61741
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54043124%2fhow-to-create-an-aggregate-based-on-unique-elements-as-fields%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown