简体   繁体   English

如何创建列表 <String> 来自Java中的mongodb中的BasicDBList?

[英]How to create a List<String> from a BasicDBList in mongodb in Java?

The JSON stored in mongodb database is of form 存储在mongodb数据库中的JSON是一种形式

{
        "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. 我是Java和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);
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM