MongoDB query to get last/all objects from array
{
WholeData:[
{
EnteredAmount:100,
OverPayment:0,
Type:"payment",
},
{
EnteredAmount:200,
OverPayment:0,
Type:"payment",
},
]
}
{
WholeData:[
{
EnteredAmount:600,
OverPayment:0,
Type:"refund",
},
{
EnteredAmount:400,
OverPayment:0,
Type:"refund",
},
]
}
This is how sample documents looks like
I want to write a query which results something like
if type is refund then push/keep all array objects else push/keep only the last index object.
sample output should be
{
WholeData:[
{
EnteredAmount:200,
OverPayment:0,
Type:"payment",
},
]
}
{
WholeData:[
{
EnteredAmount:600,
OverPayment:0,
Type:"refund",
},
{
EnteredAmount:400,
OverPayment:0,
Type:"refund",
},
]
}