简体   繁体   English

如何使用Java在mongoDB中保存和读取地图

[英]how to save and read map in mongoDB using java

i have a collection in mongo that contains 3 column as below: 我在mongo中有一个包含3列的集合,如下所示:

{ 
"_id" : { "$oid" : "5396ad5de4b09ea27a641ed6"} , 
"word" : "test_word" , 
"doc_occurrence" : "'total':25,'sport':10" ,
 "total_occurrence" : "'total':32,'sport':15"
}

doc_occurrence and total_occurrence are Map. doc_occurrence和total_occurrence是Map。 i insert documents to collection this way: 我以这种方式插入文档以进行收集:

        private boolean add(String word, Map<String, Integer> docOccurrence, Map<String, Integer> totalOccurrence) {
    BasicDBObject document = new BasicDBObject();
    document.put("word", word);
    document.put("docOccurrence", docOccurrence);
    document.put("totalOccurrence", totalOccurrence);
    try {
        synchronized (LOCK_WRITE) {
            table.insert(WriteConcern.SAFE,document);


        }
    } catch (MongoException e) {
        logger.error("Could not insert new row to word_cat : {}", e.getMessage());
        return false;
    }
    return true;
}

i want to read maps from DB this way: 我想以这种方式从数据库读取地图:

        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("word",word);
    try {
        DBCursor cursor = table.find(searchQuery);
        if (cursor.hasNext()) {
            DBObject doc = cursor.next();
            Map<String, Integer> map = (Map<String, Integer>)doc.get("docOccurrence"); // ClassCastException

but i get 但我明白了

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map

is there any way i cat read maps directly from DB? 我可以直接从数据库中读取地图吗? if not, what is altenative? 如果没有,什么是替代品?

ps: BSON convert map to String. ps:BSON将映射转换为String。 in fact BSON keys must be String! 实际上,BSON密钥必须是String!

The problem here is that you have created your BSON object with the Map part placed in the "string" size of the constructor, so there is an implicit "stringify" in there in this invocation. 这里的问题是创建的BSON对象的Map部分放置在构造函数的“字符串”大小中,因此在此调用中存在一个隐式的“ stringify”。

Wrapped as the sole argument to a BSON Object constructor then then Map serializes properly: 作为BSON Object构造函数的唯一参数包装,然后Map正确序列化:

document.put("word", word);
document.put("docOccurrence", new BasicDBObject(docOccurrence));
document.put("totalOccurrence", new BasicDBObject(totalOccurrence));

The BasicDBObject class implements the AbstractMap interface and should be able to be used that way on data that is read. BasicDBObject类实现AbstractMap接口,并且应该能够以这种方式在读取的数据上使用。 The rest of the code should work as expected. 其余代码应按预期工作。

I suggest you read more about MongoDB and NoSQL because you seem really confused about the whole concept. 我建议您阅读有关MongoDB和NoSQL的更多信息,因为您似乎对整个概念感到困惑。

You have a collection in mongodb that has a document that has 3 fields , excluding the _id . 您在mongodb中有一个集合 ,该集合文档包含3个字段 ,但_id除外。 Your doc_occurrence and total_occurrence fields should have their values as **subdocument**s(another json object) , that's how MongoDB rolls . 您的doc_occurrencetotal_occurrence字段的值应为** subdocument ** s(另一个json对象),这就是MongoDB滚动的方式。 Your usage of string csv values instead of subdocuments is completely ineffective and wrong. 您使用字符串csv值而不是子文档是完全无效和错误的。

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

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