6

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.

3
  • refer api.mongodb.org/java/current/com/mongodb/BasicDBList.html Commented May 10, 2014 at 8:07
  • i've already gone through that link I am unable to figure out how to get that list's individual elements. Commented May 10, 2014 at 8:35
  • Why does your accepting rollback? Commented May 10, 2014 at 9:04

3 Answers 3

12

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Look:

{
    "genre":  ["Action", "Animation", "Drama"],
    "movie_id": 1,
    "attributes" : [ 
        {
            "name" : "name 1",
            "value" : "value 1"
        }, {
            "name" : "name 2",
            "value" : "value 2"
        }
    ]
}

You can use:

DBObject dbObject = (DBObject) object.get("attributes");
BasicDBList list = new BasicDBList();
for (String key : dbObject.keySet()) {
    list.add(dbObject.get(key));
}

List<String> listArray = new ArrayList<String>();
for (Object object : list) {
    listArray.add(object.toString());
}

and

DBObject dbObject = (DBObject) object.get("genre");
List<String> listArray = new ArrayList<String>();
for (String key : dbObject.keySet()) {
    list.add(((DBObject) dbObject.get(key)).toString());
}

You can too (but, maybe don't working):

BasicDBList list = (BasicDBList) object.get("attributes");
List<String> listArray = new ArrayList<String>();
for (Object object : list) {
    listArray.add(((DBObject) object).toString());
}

and

BasicDBList list = (BasicDBList) object.get("genre");
List<String> listArray = new ArrayList<String>();
for (Object object : list) {
    listArray.add(object.toString());
}

Comments

-2
    DBObject channelDBObject = new BasicDBObject(); 
    System.out.println("genre");
    String genre = bufferReader.readLine();
    String[] temp = genre.split(","); 
    int i=0; 
    BasicDBList genreDBList = new BasicDBList(); 
    DBObject genreDBObject = null; 
    while(i<temp.length){ 
        genreDBObject = new BasicDBObject(); 
        genreDBObject.put("genre",temp[i++]);
        genreDBList.add(genreDBObject); 
    } 
    channelDBObject.put("genre",genreDBList.toArray());
    System.out.println("Movie Id");
    String MovieId = bufferReader.readLine();
    channelDBObject.put("MovieId",Integer.parseInt(MovieId));
    dBCollection.insert(channelDBObject); 
    DBCursor dbcursor = dBCollection.find(); 
    while (dbcursor.hasNext())System.out.println(dbcursor.next()); 
} 

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.