我在第二级数组中有重复项。
在第二级数组元素中,除_id之外,所有列都包含相同的数据。在某些情况下_id也是一样的。
请建议如何通过保留一个元素来从第二级数组中删除重复项。
从下面的示例中,我们可以将AssessmentName视为Bhra数组中的唯一字段。
Mongo版本:3.2.8 例如:
{
"_id" : ObjectId("592415c434810eeb63afe029"),
"Encounter" : [
{
"_id" : ObjectId("5846c6361489b54e402d76f6"),
"Bhra" : [
{
"_id" : "15445853419048538e0ba2cd",
"AssessmentId" : 1,
"AssessmentName" : "Major Depressive Episode",
"AssessmentOrder" : 1,
"IsContinue" : true,
"IsNotAssessed" : false,
"TotalScore" : 9,
"DepressionSeverity" : "Mild depression",
"AssessmentResult" : "Negative",
"Notes" : ""
},
{
"_id" : "15445853419048538e0ba2aa",
"AssessmentId" : 1,
"AssessmentName" : "Major Depressive Episode",
"AssessmentOrder" : 1,
"IsContinue" : true,
"IsNotAssessed" : false,
"TotalScore" : 9,
"DepressionSeverity" : "Mild depression",
"AssessmentResult" : "Negative",
"Notes" : ""
},
{
"_id" : "15445853419048538e0ba2aa",
"AssessmentId" : 1,
"AssessmentName" : "Major Depressive Episode",
"AssessmentOrder" : 1,
"IsContinue" : true,
"IsNotAssessed" : false,
"TotalScore" : 9,
"DepressionSeverity" : "Mild depression",
"AssessmentResult" : "Negative",
"Notes" : ""
}
]
}
]
}
输出应如下所示。
{
"_id" : ObjectId("592415c434810eeb63afe029"),
"Encounter" : [
{
"_id" : ObjectId("5846c6361489b54e402d76f6"),
"Bhra" : [
{
"_id" : "15445853419048538e0ba2cd",// any _id is fine
"AssessmentId" : 1,
"AssessmentName" : "Major Depressive Episode",
"AssessmentOrder" : 1,
"IsContinue" : true,
"IsNotAssessed" : false,
"TotalScore" : 9,
"DepressionSeverity" : "Mild depression",
"AssessmentResult" : "Negative",
"Notes" : ""
}
]
}
]
}
谢谢, 饶
答案 0 :(得分:0)
您可以使用聚合。首先,您需要$unwind
次数据才能达到2级数组,然后使用$group
仅包含数组中的不同项。最后,$project
为必需的json格式。
db.your_collection.aggregate([
{"$unwind":"$Encounter"},
{"$unwind":"$Encounter.Bhra"},
{"$group":{
_id: {
_id:"$_id",
"Encounter_id":"$Encounter._id",
"Encounter_Bhra_AssessmentId":"$Encounter.Bhra.AssessmentId",
"Encounter_Bhra_AssessmentName":"$Encounter.Bhra.AssessmentName",
"Encounter_Bhra_AssessmentOrder":"$Encounter.Bhra.AssessmentOrder",
"Encounter_Bhra_IsContinue":"$Encounter.Bhra.IsContinue",
"Encounter_Bhra_IsNotAssessed":"$Encounter.Bhra.IsNotAssessed",
"Encounter_Bhra_TotalScore":"$Encounter.Bhra.TotalScore",
"Encounter_Bhra_DepressionSeverity":"$Encounter.Bhra.DepressionSeverity",
"Encounter_Bhra_AssessmentResult":"$Encounter.Bhra.AssessmentResult",
"Encounter_Bhra_Notes":"$Encounter.Bhra.Notes"
},
"Encounter_Bhra_id":{"$min":"$Encounter.Bhra._id"}
}
},
{"$project":{
_id:"$_id._id",
Encounter : {
_id:"$_id.Encounter_id",
Bhra : {
_id : "$Encounter_Bhra_id",
AssessmentId : "$_id.Encounter_Bhra_AssessmentId",
AssessmentName : "$_id.Encounter_Bhra_AssessmentName",
AssessmentOrder : "$_id.Encounter_Bhra_AssessmentOrder",
IsContinue : "$_id.Encounter_Bhra_IsContinue",
IsNotAssessed : "$_id.Encounter_Bhra_IsNotAssessed",
TotalScore : "$_id.Encounter_Bhra_TotalScore",
DepressionSeverity : "$_id.Encounter_Bhra_DepressionSeverity",
AssessmentResult : "$_id.Encounter_Bhra_AssessmentResult",
Notes : "$_id.Encounter_Bhra_Notes"
}
}
}
},
{$out: "your_new_coll_name"}
])
输出:
{
"_id" : ObjectId("592415c434810eeb63afe029"),
"Encounter" : {
"_id" : ObjectId("5846c6361489b54e402d76f6"),
"Bhra" : {
"_id" : "15445853419048538e0ba2aa",
"AssessmentId" : 1,
"AssessmentName" : "Major Depressive Episode",
"AssessmentOrder" : 1,
"IsContinue" : true,
"IsNotAssessed" : false,
"TotalScore" : 9,
"DepressionSeverity" : "Mild depression",
"AssessmentResult" : "Negative",
"Notes" : ""
}
}
}