问题
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 like status,erroCode,deviceName etc.
I want to create the group for deviceName in the report when I create the group, it is created in this form :
[ "CardReader" , "CashAcceptor" , "CashDispenser" , "ChequeAcceptor" , "EmiratesIdScanner" , "PinPad" , "JournalPrinter" , "ReceiptPrinter" , "StatementPrinter" , "SignpadScanner"]
I want it to be grouped in the broken form like :
CardReader
CashAcceptor
CashDispenser ..
Also the errorCode is coming in this form from data set :
[ "97080301" , "97080302,97080303" , "" , "" , "" , "" , "" , "" , "" , ""]
In the same way I want to break the error code and status code from array form to each row for each deviceName like.
CardReader 97080301
CashAcceptor 97080302,97080303
CashDispenser
I am new to birt , please someone help. thanks in advance.
After Applying Veeram's answer , errorCode is not showing in the report but its there in preview results :
1 :
2 :
回答1:
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":""}}
来源:https://stackoverflow.com/questions/47752325/creating-group-and-analyzing-data-from-array-list-in-birt