I have some documents listed in a stage of aggregation pipeline like this:
[{
obj1:{code:"123"},
obj2:{code:"654"},
obj3:{code:"abc"},
obj4:{code:"xyz"},
type:"obj1",
},{
obj1:{code:"123"},
obj2:{code:"654"},
obj3:{code:"abc"},
obj4:{code:"xyz"},
type:"obj2",
},{
obj1:{code:"123"},
obj2:{code:"654"},
obj3:{code:"abc"},
obj4:{code:"xyz"},
type:"obj4",
}]
In the same aggregation pipeline, Without using $switch cases, is it possible that in each objects we project the code field for it's respective type value in $project stage?
Result should be:
[{
obj1:{code:"123"},
obj2:{code:"654"},
obj3:{code:"abc"},
obj4:{code:"xyz"},
type:"obj1",
code:$type.code // should return "123"
},{
obj1:{code:"123"},
obj2:{code:"654"},
obj3:{code:"abc"},
obj4:{code:"xyz"},
type:"obj2",
code:$type.code // should return "654"
},{
obj1:{code:"123"},
obj2:{code:"654"},
obj3:{code:"abc"},
obj4:{code:"xyz"},
type:"obj4",
code:$type.code // should return "xyz"
}]
Thank you in Advance.