API Java MongoDB:异步模式下insertOne()中的编译时错误
我正在研究MongoDB的JavaAPI编码一组单元测试,在理解同步/异步模式下的模型时遇到了一些困难 在feature/create test a中,我在我的pom中声明了这两种模式的驱动程序(artifactId:mongodb驱动程序同步和mongodb驱动程序反应流),我没有使用artifactIdmongo java驱动程序,因为我在Stackoverflow中读取数据,这不是一种好的使用方法 我不使用mongodb驱动程序异步,因为在类com.mongodb.async.SingleResultCallback()的javadoc中,我读到它已被弃用 我尝试以异步方式测试文档的插入,并在我的特定方法net.alepuzio.mymongodbkata.Connection.insertOneDocumentInAsyncWay(MongoClient,String,String,Document)中使用这些指令API Java MongoDB:异步模式下insertOne()中的编译时错误,java,mongodb,async-await,compiler-errors,Java,Mongodb,Async Await,Compiler Errors,我正在研究MongoDB的JavaAPI编码一组单元测试,在理解同步/异步模式下的模型时遇到了一些困难 在feature/create test a中,我在我的pom中声明了这两种模式的驱动程序(artifactId:mongodb驱动程序同步和mongodb驱动程序反应流),我没有使用artifactIdmongo java驱动程序,因为我在Stackoverflow中读取数据,这不是一种好的使用方法 我不使用mongodb驱动程序异步,因为在类com.mongodb.async.Single
public void insertOneDocumentInAsyncWay(MongoClient-MongoClient,字符串数据库,
字符串集合,文档(新文档){
mongoClient.getDatabase(数据库)
.getCollection(集合)
.insertOne(newDocument,//编译时错误
新的SingleResultCallback(){
@凌驾
公开作废结果(最终作废结果、最终可丢弃结果){
System.out.println(“插入!”);
}
});
但是在Collection.insertOne(…)中,我收到了编译时错误的消息
类型中的方法insertOne(文档,InsertOnOptions)
MongoCollection不适用于参数
(文档,新的SingleResultCallback(){})
我认为我有不正确的包导入,但是,为了寻找正确的类,我在两个驱动程序中发现了相同的insertOne()方法签名
- InsertOneResult insertOne(客户端会话客户端会话,t文档文档)
- InsertOneResult insertOne(客户端会话客户端会话、t文档文档、插入选项)
- InsertOneResult insertOne(t文件)
- InsertOneResult insertOne(t文档文档,插入选项)
public void insertOneDocumentInAsyncWay(MongoClient mongoClient, String database,
String collection, Document newDocument) {
mongoClient.getDatabase(database)
.getCollection(collection)
.insertOne(newDocument, //Compile time error
new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
System.out.println("Inserted!");
}
});