I've been trying to get this query, but I have been unable to roll my heads over this one.
The Object looks like this:
{
restaurantName:'abc',
reviews:[
{
text:'Its awesome!'
person: 'John Doe'
}
{
text:'Nice Ambience'
person: 'Davis'
}
]
}
The intent is to find out all the reviews text present in the document. How do I do this using JAVA driver for Mongo. I keep getting Class Cast Exceptions
Following is the code:
Set fields = new TreeSet();
BasicDBList e ;
try {
DBObject dbObject;
while (cursor.hasNext()) {
doc = new Document();
e = (BasicDBList) cursor.next().get("reviews");
for(BasicDBObject temp: e){
fields.addAll(temp.keySet());
}
}
//System.out.println(cursor.next());
}
The error I am getting is:
Exception in thread "main" java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to java.util.ArrayList at org.poly.darshan.utils.DataExtractor.main(DataExtractor.java:57)
The code mentioned above intends to find the unique fields present in the sub-objects. But the problem is more or less the same.