【问题标题】:Mongo Database save data from MapMongo数据库从地图保存数据
【发布时间】:2011-10-12 03:47:21
【问题描述】:

我有以下有效的代码:

if (aDBCursor.hasNext()) {
    DBObject aDbObject = aDBCursor.next();
    aDbObject.put("title", "Test Title");
    ArrayList<DBObject> department = new ArrayList<DBObject>();

    DBObject nested1 = new BasicDBObject();
    nested1.put("name", "Department A");
    nested1.put("id", 1);
    department.add(nested1);

    DBObject nested2 = new BasicDBObject();
    nested2.put("name", "Department B");
    nested2.put("id", 2);
    department.add(nested2);

    aDbObject.put("department", department);
    collection.save(aDbObject);
}

但是,我在如下地图中有 A 部门和 B 部门的数据:

Map<Object,Object> map = new HashMap<Object,Object>();
map.put("1", "Department A");
map.put("2", "Department B");

保存这些数据最好/最简单的方法是什么?有没有办法将地图直接放入 mongo DB?还是我必须在地图上循环?

进入地图的数据已经从数据库中获取,如下所示:

String[] values = req.getParameterValues("departments");
Map<Object,Object> map = new HashMap<Object,Object>();

DBCollection collection = database.getCollection("Departments");
BasicDBObject query = new BasicDBObject();
query.put("id", new BasicDBObject("$in", values));
DBCursor cursor = collection.find(query);   

如果我可以将 DBCursor 对象放回数据库,那就更好了。

有什么想法吗?

感谢您的任何帮助或建议!

【问题讨论】:

    标签: java mongodb map mongo-java


    【解决方案1】:

    本机 Java 类型(intfloatStringDateMap, 等)将自动编码为正确的 BSON 类型,因此您可以使用 BasicDBObject 将 @ 987654328@直接进入mongo收藏:

    // you probably want to be more specific with your generics than Object!
    Map<Object,Object> map = new HashMap<Object,Object>();
    map.put("1", "Department A");
    map.put("2", "Department B");
    collection.insert(new BasicDBObject(map));
    

    但是,看起来您的 Map 实际上并没有您想要的结构,因此您需要某种映射到所需结构。要么使用内置在 java 驱动程序中的基本映射(调用BasicDBObject.put 就在正确的轨道上,here 有更多想法),或者使用像 Morphia 之类的东西进行扩展映射。

    【讨论】:

      【解决方案2】:

      What would the best/easiest way be to save this data? Is there a way to put the map straight into the mongo DB? Or would I have to loop over the map? Map可以直接通过构造函数本身添加到BasicDBObject中,可以直接插入到db中,无需迭代。

      Would be even better is I could just put the DBCursor object back into the database.
      

      DBCursor 实现了迭代器,所以不迭代就不能放回db中

      【讨论】:

        【解决方案3】:

        好的,伙计们,我搞定了。

        String[] values = req.getParameterValues("departments");
        Map<Object,Object> map = new HashMap<Object,Object>();
        
        DBCollection collection = database.getCollection("Departments");
        BasicDBObject query = new BasicDBObject();
        query.put("id", new BasicDBObject("$in", values));
        DBCursor cursor = collection.find(query); 
        
        
        
        if(aDBCursor.hasNext()){
                DBObject aDbObject=aDBCursor.next();
                aDbObject.put("title", "Test Title");
                aDbObject.put("department", cursor);
                collection.save(aDbObject);
            }
        

        就这么简单!

        感谢您的所有回复和建议!

        【讨论】:

          猜你喜欢
          • 2020-01-06
          • 2013-09-15
          • 2023-02-25
          • 1970-01-01
          • 1970-01-01
          • 2013-12-24
          • 2015-10-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多