Question

I need to get a list of all the name fields from the following json. I'm trying to do this by mapping a DBObject.

JSON:

{
    "firstOne": [
        {
            "file": "FileA",
            "Data": "One",
            "version": "0.4"
        }
    ],
    "secondOne": [
        {
            "elementName": "version",
            "complexElement": "true",
            "elementDataType": ""
        }
    ],
    "ThirdOne": [
        {
            "elementName": "version",
            "explicitElements": [
                {
                    "name": "mytag",
                    "type": "String",
                    "value": "myrequest"
                },
                {
                    "name": "booleantest",
                    "type": "Boolean",
                    "value": "true"
                }
            ]
        }
    ] }

CODE:

DBCollection collection = mongoTemplate.getCollection("testTag");

  Map<DBObject, DBObject> map = new HashMap<DBObject, DBObject>();
  DBObject obj =null;

  BasicDBObject query = new BasicDBObject();
  query.put("ThirdOne.explicitElements.name", "myTag");
  BasicDBObject fields = new BasicDBObject();
  fields.put("ThirdOne.explicitElements.name", 1);
  fields.put("ThirdOne.explicitElements.value", 1);

  DBCursor cursor2 = collection.find(query, fields);      while

(cursor2.hasNext()) { obj = cursor2.next(); map = obj.toMap();

      for (Map.Entry<DBObject, DBObject> entry : map.entrySet()) {
          System.out.println("Key : " + entry.getKey() + " Value : "
                  + entry.getValue());


      }
              }

RESULTS:

key: _id Value: 523f910681a9535f8af5aa91

Key: ThirdOne Value: [{ "explicitElements" : [{"name":"mytag", "value":"myrequest"},{"name":"booleantest", "value":"true"}]}]

I would like to get only

key: "name" value: "mytag"

key: "value" value: "myrequest" etc...

Any suggestions on how to do this using DBObjects?

Was it helpful?

Solution

What you have here is a bunch of value-fields stored in DBObjects stored in DBLists stored in DBObjects stored in DBLists stored in DBObjects iterated by a DBCursor. Due to this convoluted document-structure you need to walk through this whole tree when you want to output only those on a specific leaf of it.

while (cursor2.hasNext()) {
     // you are now iterating a list objects with three Fields:
     // FirstOne, SecondOne and ThirdOne. You only want ThirdOne
     obj = cursor2.next();
     map = obj.toMap();
     DBList thirdOne = (DBList)map.get("ThirdOne");
     // now we iterat the list in ThirdOne
     for (Object o: thirdOne) {
           DBObject thirdOneEntry = (DBObject)o;     
           // we get the explicitElements-array from each entry    
           DBList explicitElements = (DBList)thirdOneEntry.get("explicitElements");
           // now we iterate the expliciteElements         
           for (Object o: explicitElements) { 
               DBObject explicitElementsEntry = (DBObject)o;
               // and write them
               Object key = explicitElementsEntry.get("key");
               Object value = explicitElementsEntry.get("value");
               System.out.println("Key : " + key.toString() + " Value : " value.toString());
           }
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top