El JSON que se almacenan en la base de datos mongodb es de forma

{
        "genre":  ["Action", "Animation", "Drama"],
        "movie_id": 1
}

Tengo que conseguir una lista de géneros. Lo siento si la pregunta es cojo. Yo soy un poco nuevo en Java y mongodb.

InformationsquelleAutor khirod | 2014-05-10

3 Comentarios

  1. 10

    propongo el siguiente código para resolver el problema:

    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);
    }
  2. 0

    Aspecto:

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

    Puede utilizar:

    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());
    }

    y

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

    Usted también puede (pero, tal vez no de trabajo):

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

    y

    BasicDBList list = (BasicDBList) object.get("genre");
    List<String> listArray = new ArrayList<String>();
    for (Object object : list) {
        listArray.add(object.toString());
    }
  3. -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()); 
    } 

Dejar respuesta

Please enter your comment!
Please enter your name here