0

I am trying to retrieve one particular field("Versions.id") from mongo using java

Mongo query - db.getCollection('SettlementInstance').find({"_id.timeSlice" : [2018,1,1], "_id.type" : "TRANSMISSION"})

This is the data stored in mongo collection. I want to retrieve "id" : "3d8a3b31-aaa0-4c2e-82b3-5eaaa80d80e1" using java

   {
            "_id" : {
                "timeSlice" : [ 
                    2018, 
                    1, 
                    1
                ],
                "type" : "TRANSMISSION",
                "@objectName" : "SettlementInstance"
            },
            "Versions" : [ 
                {
                    "id" : "3d8a3b31-aaa0-4c2e-82b3-5eaaa80d80e1",
                    "status" : "ACTIVE",
                    "version" : NumberLong(11447)
                }
            ]
        }

The Java program I was trying to retrieve the "id" field under "Versions" object, my code gives an exception.

String feedName = "ServicePointInputAdapter";
            Mongo mongo = new Mongo(host);
            DB db = mongo.getDB(("dbname"));
            DBCollection collection = db.getCollection("SettlementInstance");
            BasicDBObject whereQuery = new BasicDBObject();
            whereQuery.put("_id.type", "TRANSMISSION");
            whereQuery.put("_id.timeSlice", Arrays.asList(2018,1,1));
            DBCursor cursor = collection.find(whereQuery);
            try {
                while (cursor.hasNext()) {
                    DBObject Features = cursor.next();
                    BasicDBList features = (BasicDBList) Features.get("Versions");
                    BasicDBObject[] featuresArr = features.toArray(new BasicDBObject[0]);
                    for (BasicDBObject dbobj : featuresArr) {
        *****                BasicDBObject si_id = (BasicDBObject) dbobj.get("id"); ***** //Getting error in the line }
                }
            } finally {
                cursor.close();
            }
        }
    } 

Unfortunately I am getting exception such as -

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.BasicDBObject.

Could some one help me resolve the issue and retrieve the "Versions.id" field from the collection.

2 Answers 2

1

As the error clearly says that you are trying to cast a java.lang.String to com.mongodb.BasicDBObject which is throwing java.lang.ClassCastException

Change the line of code as below (as BasicDBObject implements HashMap):

for (BasicDBObject dbobj : featuresArr) {
    String id = (String)((HashMap)dbobj).get("id");
    System.out.println(id);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I already tried as String and tried to print the si_id - The program printed whole part under Versions such as - { "id" : "3d8a3b31-aaa0-4c2e-82b3-5eaaa80d80e1" , "status" : "ACTIVE" , "version" : 11447} I specifically just want the "id" field to be printed.
Typecast the BasicDBObject to HashMap and get id out of it.
@janopan Thanks for marking the answer as correct. Also, can you please upvote it?
stackoverflow.com/questions/49699907/… Can you try to answer this on mongoid field (_id). I just want to get/print "_id.employeeId" field only
0

This get() method only returns the value. So it was a string when you do this

dbobj.get("id")

You should pass the field name as the parameter and you will get the value. So change this line like below

BasicDBObject si_id = (BasicDBObject) dbobj.get("id");

to 
String dbObjectId = dbobj.get("id");

2 Comments

I already tried as String and tried to print the si_id - The program printed whole part under Versions such as - { "id" : "3d8a3b31-aaa0-4c2e-82b3-5eaaa80d80e1" , "status" : "ACTIVE" , "version" : 11447} I specifically just want the "id" field to be printed
have you tried using only this DBObject Features = cursor.next(); in hasNext() loop?. You can directly get the results there.

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.