0

I want to extract all these places mentioned in "location" field and does not want the other fields in the below json.but can't be able to extract since it is nested..Can anyone help me?

    DBCursor cursorTotal = coll.find(obje);

    while (cursorTotal.hasNext()) {

        DBObject curNext = cursorTotal.next();

        System.out.println("data::"+curNext.get("list.myList.location");
   }

My "curNext" gives output as::

    {
    "_id": {
    "$oid": "51ebe983e4b0d529b4df2a0e"
    },
    "date": {
    "$date": "2013-07-21T13:31:11.000Z"
    },
    "lTitle": "Three held for running CISF job racket",
    "list": {
    "myList": [
        {
            "location": "Germany"
        },
        {
            "location": "Geneva"
        },
        {
            "location": "Paris"
        }
    ]
    },
    "hash": -1535814113,
    "category": "news"
}

I want my output as

Germany,Geneva,Paris
2
  • What did you try so far? Commented Apr 7, 2015 at 7:15
  • @BetaRide i have updated my question and added one of my trial Commented Apr 7, 2015 at 7:19

2 Answers 2

2

I have been in a long wait here for an answer and finally I got what I was searching for...Just noting my answer so someone else can benefit from it

  DBCursor cursorTotal = coll.find(obje);

    while (cursorTotal.hasNext()) {

    DBObject curNext = cursorTotal.next();

            String res=curNext.toString();
           JsonElement jelement = new JsonParser().parse(res);
            JsonObject  jobject = jelement.getAsJsonObject();
            jobject = jobject.getAsJsonObject("list");
            JsonArray jarray = jobject.getAsJsonArray("myList");
            jobject = jarray.get(0).getAsJsonObject();
            String result = jobject.get("location").getAsString();


            System.out.println("all places::"+result);
}
Sign up to request clarification or add additional context in comments.

Comments

0

For finding only locations you should used mongo aggregation, below query will fetch all lcoations array

    db.collectionName.aggregate({
    "$unwind": "$ner.nerList"
},
{
    "$group": {
    "_id": "$_id",
    "location": {
        "$push": "$ner.nerList.location"
    }
    }
},
{
    "$project": {
    "location": "$location",
    "_id": 0
    }
})

Unfortunately I don't know how to convert this in Java but, I find below links which helpfull to you for converting above query in java format Mongo Java aggregation driver

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.