0
public static void main (String args[]) 
    {
        GetMorphiaDB morphia;
        //String id  = request.getParameter("id");
        try {
            morphia = GetMorphiaDB.getInstance();
            Map<String,Object> output = new LinkedHashMap<String, Object>();
            DB db = morphia.getDb();
            DBCollection collection = db.getCollection("Transactions");
            BasicDBObject allQuery = new BasicDBObject();
              BasicDBObject fields = new BasicDBObject();
              fields.put("referenceID", 1);

              /*DBCursor cursor2 = collection.find(allQuery, fields);
              while (cursor2.hasNext()) {
                System.out.println(cursor2.next());
              }*/

              //System.out.println("\n2. Find where number = 5");
              BasicDBObject whereQuery = new BasicDBObject();
              whereQuery.put("referenceID", "E13F31BC80CE");
              fields.put("referenceID", 1);
              //fields.put("listEditions", 1);
              fields.put("listEditions.listInsertion", 1);
              fields.append("_id",false);
              DBCursor curs = collection.find(whereQuery,fields);
              while (curs.hasNext()) {
                System.out.println(curs.next());
              }
} 
        catch (UnknownHostException e) 
        {
            e.printStackTrace();
        } 
        catch (MongoException e) {
            e.printStackTrace();
        }

    }

I have above program which gives me output as below

{ "listEditions" : [ { "listInsertion" : [ { "page" : 2 , "fromDate" : "11/06/2013" , "toDate" : "11/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0} , { "page" : 2 , "fromDate" : "11/06/2013" , "toDate" : "11/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0}]} , { "listInsertion" : [ { "page" : 2 , "fromDate" : "14/06/2013" , "toDate" : "14/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0} , { "page" : 2 , "fromDate" : "14/06/2013" , "toDate" : "14/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0}]} , { "listInsertion" : [ { "page" : 2 , "fromDate" : "11/06/2013" , "toDate" : "11/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0} , { "page" : 2 , "fromDate" : "11/06/2013" , "toDate" : "11/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0}]} , { "listInsertion" : [ { "page" : 2 , "fromDate" : "11/06/2013" , "toDate" : "11/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0} , { "page" : 2 , "fromDate" : "11/06/2013" , "toDate" : "11/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0}]} , { "listInsertion" : [ { "page" : 2 , "fromDate" : "15/06/2013" , "toDate" : "15/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0} , { "page" : 2 , "fromDate" : "15/06/2013" , "toDate" : "15/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0}]}] , "referenceID" : "E13F31BC80CE"}

Now I want to fetch values from innerDocument listInsertion which is inside listEditions in [] brackets.

So how to get it.

2 Answers 2

1

You can use something like this:

DBObject next = cursor.next();
BasicDBList listEditions = (BasicDBList)next.get("listEditions");
for(Object element: listEditions) {
    BasicDBList listInsertions = (BasicDBList)((BasicDBObject)element).get("listInsertion");
    for(Object lie: listInsertions) {
        System.out.println(lie);
        //System.out.println(((BasicDBObject)lie).get("fromDate"));
    }
}

Add instanceof and other checks if you need.

Sign up to request clarification or add additional context in comments.

Comments

1

That's JSON and you can parse it using a standard JSON library, or map it to a Java object using GSON.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.