如何从 Java mongodb 中的 BasicDBList 创建 List<String>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23578637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:05:44  来源:igfitidea点击:

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

javajsonmongodb

提问by khirod

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 有点陌生。

采纳答案by jmen7070

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

回答by user2791631

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

回答by Alberto Cerqueira

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