fmartin1987 Posted January 3, 2017 Share Posted January 3, 2017 Please consider a MongoDB collection with the following document: "_id": "clientsInfo" "data": { "clientsList" : [ { "name" : "Mike", "country" : "USA" }, ... ] }After setting the DataSet and defining the Query like this... { collectionName:'projectA', findQuery: { '_id':'clientsInfo', }, findFields: { '_id':0, 'data.clientsList':1 }, }...I am able to display the first item of the fetched array (`java.util.List` type) in JasperSoft Studio inside a Text Field using the following expression:But, considering that I would like to exhibit the whole data in a Name/Country Table... Question1: How could I access any of the dictionary fields? Trying [get method][1] I obtain The method get(String) is undefined for the type Object. error. However, knowing that the object is an instance of com.mongodb.BasicDBObject it should have that method inherited (See [doc][2]). I have also tried to cast object to org.json.JSONObject but then I get net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: (JSONObject)$F{data.clientsList}.get(0) error. Question2: Let's suppose we have already solved first question... How can I iterate the list to access not only the first item but all of them according to the array length? Is it possible to use for-loop sentence inside the JasperSoft Expression Editor? ([if-then-else seems to be available][3])Thanks in advance, Any clue that point me in the right direction will be appreciated. [1]: http://stackoverflow.com/questions/9607562/how-to-access-dictionary-data-within-an-arraylist-in-java [2]: http://api.mongodb.com/java/2.0/com/mongodb/BasicDBObject.html [3]: http://community.jaspersoft.com/documentation/tibco-jaspersoft-studio-user-guide/v60/expressions Note: If someone wants to earn StackOverflow reputation, question is also placed there: http://stackoverflow.com/questions/41396004/which-is-the-proper-way-to-work-with-complex-types-lists-and-objects-in-jasper Link to comment Share on other sites More sharing options...
fmartin1987 Posted January 25, 2017 Author Share Posted January 25, 2017 Just in case someone was in the same situation as I was, I must say this whole approach was wrong. It's not about making a simple query which returns big block of complex data formatted as an object or list of objects and then manipulate it with JasperSoft Studio. Instead, what I had to do was design a more elaborated query which returns the simple fields I wanted to use straightforward. How to do this? By using Aggregation Framework. So, by changing this... { collectionName:'projectA', findQuery: { '_id':'clientsInfo', }, findFields: { '_id':0, 'data.clientsList':1 }, } ...for this... { runCommand: { aggregate : 'projectA', pipeline : [ {'$match': {'_id':'clientsInfo'}}, {'$project': {'data.clientsList': 1}}, {'$unwind': '$data'}, {'$unwind': '$data.clientsList'} ] } } ...is how I get `name` and `country` fields in order to use them in Text Fields, Tables, ...etc. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now