The JSON stored in mongodb database is of form
{
"genre": ["Action", "Animation", "Drama"],
"movie_id": 1
}
I have to get a list of genres. Sorry if the question is lame. I'm kinda new to Java and mongodb.
i propose the below code to solve your issue:
MongoClient mongo = new MongoClient( "localhost" , 27017 );
DB db = mongo.getDB(dbName);
DBCollection collection = db.getCollection(collectionName);
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("movie_id", id);
DBObject document = collection.findOne(whereQuery);
BasicDBList list = (BasicDBList) document.get("genre");
List<String> res = new ArrayList<String>();
for(Object el: list) {
res.add((String) el);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With