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.