我在Java中使用Mongodb并尝试创建文档:

// mongo-java-driver-2.10.1 version
// create a document to store key and value
            BasicDBObject document = new BasicDBObject();
            document.put("name", "Yassine LD");

            BasicDBObject documentDetail = new BasicDBObject();
            documentDetail.put("addressLine1", "Sweet Home");
            documentDetail.put("addressLine2", "New Street");
            documentDetail.put("addressLine3", "CASABLANCA, MOROCCO");

            document.put("address", documentDetail);

            table.insert(document);


我收到此错误:类型DBCollection中的方法insert(DBObject [])不适用于参数(BasicDBObject)

最佳答案

看来您的insert方法需要一个DBObject数组。假设BasicDBObjectDBObject的子类,则可以编写:

table.insert(new DBObject[] {document});

10-08 17:41