I am using mongo db in birt reports. I have data set which contains an array of object like in the picture.
deviceStatus is an array which contains child object
Please follow below steps to get the desired response.
Data:
db.devicestatus.insert([
{
"_id": "0001",
"className":"store",
"deviceStatus": [ {
"deviceName": "CardReader",
"errorCode": "97080301",
"status": "Bad"
},
{
"deviceName": "CashAcceptor",
"errorCode": "97080302,97080303",
"status": "Bad"
},
{
"deviceName": "CashDispenser",
"errorCode": "",
"status": "Good"
}]
}
])
1.Data Explorer - Go to Data Sets - New Data Set - Select Data Source - Input Data Set Name - Click Next
2.Input collection name - devicestatus - Lists all the fields - Select Aggregate option from the command type dropdown - Click Expression
3.Add the below expression in the expression builder prompt - Click OK
The below expression $unwind to flatten the array to decompose devicestatus array into documents followed by $project to keep the required fields.
[
{"$unwind":"$deviceStatus"},
{"$project":{
"_id":0,
"className":1,
"deviceStatus.deviceName":1,
"deviceStatus.errorCode":1
}
}
]
OR
The below expression iterates over devicestatus array and $map and $project the required fields followed by $unwind to flatten to decompose the array into documents.
[{
"$project":{
"_id":0,
"className":1,
"deviceStatus":{
"$map":{
"input":"$deviceStatus",
"as":"result",
"in":{
"deviceName":"$$result.deviceName",
"errorCode":"$$result.errorCode"
}
}
}
}
},
{"$unwind":"$deviceStatus"}
]
OR
4.Confirm to refresh - Click yes
5.Move all available fields to the selected multi select drop box - Click Finish
6.Preview Results
{"className":"store", "deviceStatus":{"deviceName":"CardReader","errorCode":"97080301"}}
{"className":"store", "deviceStatus":{ "deviceName":"CashAcceptor","errorCode":"97080302,97080303"}}
{"className":"store","deviceStatus":{"deviceName":"CashDispenser","errorCode":""}}