MongoDB 3.2.1 Java BasicDBObjectBuilder

时间:2016-02-13 04:13:17

标签: java mongodb

当我尝试将BasicDBObjectBuilder插入集合

时,我收到以下错误
  

引起:java.lang.ClassCastException:com.mongodb.BasicDBObject无法强制转换为com.mongodb.BasicDBObjectBuilder

    MongoClient mongo = new MongoClient( "xxx.xxx.x.x" , 27017 );
    MongoDatabase db = mongo.getDatabase("hello"); //create database

    MongoCollection<BasicDBObjectBuilder> collbuild = db.getCollection("newcoll", BasicDBObjectBuilder.class);
    BasicDBObjectBuilder collbuilder =  BasicDBObjectBuilder.start()
            .add("name","joe")
            .add("dob", "12/12/12")
            .add("country", "Utopia");
    collbuild.insertOne((BasicDBObjectBuilder) collbuilder.get()); // passing BasicDBObjectBuilder into a BasicDBObjectBuilder collection?

我以为我将BasicDBObjectBuilder传递给BasicDBObjectBuilder集合。我知道如何使用BasicDBObject,但我想尝试这种方法,因为我是编程新手。有人可以澄清吗?

  

已更新

似乎我试图使用insertOne这是一个BasicDBObject函数。那么将BasicDBObjectBuilder传递给BasicDBObjectBuilder集合需要什么呢?

0 个答案:

没有答案