Input:
{
"_id" : ObjectId("62b2beb8bfe272962f274942"),
"CDF" : {
"UTILITYTYPE" : {
"D1" : {
"G1" : "12387835",
"G2" : "02-06-2022 13:03:23",
"G22" : {
"NAME" : "LARSEN AND TOUBRO LIMITED"
}
},
"D5" : {
"EVENT" : [
{
"CODE" : "14",
"TIME" : "23-05-2022 09:23:39",
"STATUS" : "0",
"SNAPSHOT" : [
{
"PARAMCODE" : "P1-2-1-1-0",
"VALUE" : "218.39",
"UNIT" : "V"
},
{
"PARAMCODE" : "P1-2-2-1-0",
"VALUE" : "243.67",
"UNIT" : "V"
},
{
"PARAMCODE" : "P1-2-3-1-0",
"VALUE" : "241.37",
"UNIT" : "V"
},
{
"PARAMCODE" : "P2-1-1-1-0",
"VALUE" : "0.456",
"UNIT" : "A"
},
{
"PARAMCODE" : "P2-1-2-1-0",
"VALUE" : "0",
"UNIT" : "A"
},
{
"PARAMCODE" : "P2-1-3-1-0",
"VALUE" : "0",
"UNIT" : "A"
},
{
"PARAMCODE" : "P4-1-1-0-0",
"VALUE" : "-0.99",
"UNIT" : "NA"
},
{
"PARAMCODE" : "P4-2-1-0-0",
"VALUE" : "0",
"UNIT" : "NA"
},
{
"PARAMCODE" : "P4-3-1-0-0",
"VALUE" : "0",
"UNIT" : "NA"
},
{
"PARAMCODE" : "P7-1-5-2-0",
"VALUE" : "2383.03",
"UNIT" : "k"
}
]
},
{
"CODE" : "14",
"TIME" : "23-05-2022 10:40:49",
"STATUS" : "1",
"SNAPSHOT" : [
{
"PARAMCODE" : "P1-2-1-1-0",
"VALUE" : "227.58",
"UNIT" : "V"
},
{
"PARAMCODE" : "P1-2-2-1-0",
"VALUE" : "236.78",
"UNIT" : "V"
},
{
"PARAMCODE" : "P1-2-3-1-0",
"VALUE" : "236.78",
"UNIT" : "V"
},
{
"PARAMCODE" : "P2-1-1-1-0",
"VALUE" : "0",
"UNIT" : "A"
},
{
"PARAMCODE" : "P2-1-2-1-0",
"VALUE" : "0",
"UNIT" : "A"
},
{
"PARAMCODE" : "P2-1-3-1-0",
"VALUE" : "0",
"UNIT" : "A"
},
{
"PARAMCODE" : "P4-1-1-0-0",
"VALUE" : "0",
"UNIT" : "NA"
},
{
"PARAMCODE" : "P4-2-1-0-0",
"VALUE" : "0",
"UNIT" : "NA"
},
{
"PARAMCODE" : "P4-3-1-0-0",
"VALUE" : "0",
"UNIT" : "NA"
},
{
"PARAMCODE" : "P7-1-5-2-0",
"VALUE" : "2383.16",
"UNIT" : "k"
}
]
}
]
}
}
}
}
Expected output:
{
"_id" : ObjectId("62b2beb8bfe272962f274942"),
"CDF" : {
"UTILITYTYPE" : {
"D1" : {
"G1" : "12387835",
"G2" : "02-06-2022 13:03:23",
"G22" : {
"NAME" : "LARSEN AND TOUBRO LIMITED"
}
},
"D5" : {
"EVENT" : [
{
CODE:14,
TIME :23-05-2022 09:23:39,
STATUS: 0,
"SNAPSHOT" : [
{
"PARAMCODE" : "P1-2-1-1-0",
"VALUE" : "218.39",
"UNIT" : "V"
},
{
"PARAMCODE" : "P1-2-2-1-0",
"VALUE" : "243.67",
"UNIT" : "V"
},
{
"PARAMCODE" : "P1-2-3-1-0",
"VALUE" : "241.37",
"UNIT" : "V"
}
]
},
{
CODE:14,
TIME :23-05-2022 10:40:49,
STATUS: 0,
"SNAPSHOT" : [
{
"PARAMCODE" : "P1-2-1-1-0",
"VALUE" : "227.58",
"UNIT" : "V"
},
{
"PARAMCODE" : "P1-2-2-1-0",
"VALUE" : "236.78",
"UNIT" : "V"
},
{
"PARAMCODE" : "P1-2-3-1-0",
"VALUE" : "236.78",
"UNIT" : "V"
}
]
}
]
}
}
}
}
I am using below $add Fields
{
"CDF.UTILITYTYPE.D5.EVENT": {
$map: {
input: {
$filter: {
input: "$CDF.UTILITYTYPE.D5.EVENT",
cond: {
$in: [
"V",
"$$this.SNAPSHOT.UNIT"
]
}
}
},
in: {
"SNAPSHOT": {
$filter: {
input: "$$this.SNAPSHOT",
as: "d",
cond: {
$eq: [
"$$d.UNIT",
"V"
]
}
}
}
}
}
}
}
which removes the STAUS , TIME AND CODE from my final result
How can i keep STAUS , TIME AND CODE from my final result?
CodePudding user response:
One option is to use $mergeObjects
(and simplify the query a bit):
db.collection.aggregate([
{
$set: {
"CDF.UTILITYTYPE.D5.EVENT": {
$map: {
input: "$CDF.UTILITYTYPE.D5.EVENT",
as: "event",
in: {
$mergeObjects: [
"$$event",
{
SNAPSHOT: {
$filter: {
input: "$$event.SNAPSHOT",
as: "d",
cond: {$eq: ["$$d.UNIT", "V"]}
}
}
}
]
}
}
}
}
}
])
See how it works on the playground example