【问题标题】:Compile error when using Block<Document> to print collection using Java MongoDB 4.0.4 driver使用 Java MongoDB 4.0.4 驱动程序使用 Block<Document> 打印集合时出现编译错误
【发布时间】:2020-10-12 18:27:05
【问题描述】:

使用Java MongoDB Driver 4.0.4、OpenJDK 11 并按照MongoDB docs 上的示例,使用findIterable.forEach(printBlock); 时看到编译错误

看起来com.mongodb.Block http://mongodb.github.io/mongo-java-driver/4.0/driver/tutorials/aggregation/ 也没有被弃用。

public String testLocal() {
  MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
  MongoDatabase database = mongoClient.getDatabase("cord");
  MongoCollection<Document> collection1 = database.getCollection("papers");

  FindIterable<Document> findIterable = collection1.find(new Document());
  findIterable.forEach(printBlock);

}

public Block<Document> printBlock = new Block<Document>() {
  @Override
  public void apply(final Document document) {
    System.out.println(document.toJson());
  }
};

错误:

[ERROR] /Projects/cord/src/main/java/com/engg/java/cord/services/PrimaryService_Local.java:
[24,30] incompatible types: com.mongodb.Block<org.bson.Document> cannot be converted
 to java.util.function.Consumer<? super org.bson.Document>

【问题讨论】:

  • 也许您需要将findIterable.forEach(printBlock); 改为findIterable.forEach(doc -&gt; printBlock.apply(doc));?

标签: java mongodb mongodb-java


【解决方案1】:

文档显然已经过时了。如果您查看 FindIterable 的 javadocs (http://mongodb.github.io/mongo-java-driver/4.0/apidocs/mongodb-driver-sync/com/mongodb/client/FindIterable.html) 你会看到forEach 继承自java.lang.Iterable,它接受Consumer 作为参数,而不是Block。

因此,将您的 printBlock 声明替换为:

public Consumer<Document> printBlock = document -> System.out.println(document.toJson());

...一切都会好起来的。

【讨论】:

    【解决方案2】:

    在 mtj 和 Mongodb 支持的帮助下,添加工作代码块..

    public String testLocal() {
      MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
      MongoDatabase database = mongoClient.getDatabase("cord");
      MongoCollection<Document> collection1 = database.getCollection("papers");
    
      FindIterable<Document> findIterable = collection1.find(new Document());
      findIterable.forEach((Consumer<Document>)  d -> System.out.println( d.toJson())); 
    }
    

    或

    public Consumer<Document> printBlock = document -> System.out.println(document.toJson());
    
    public String testLocal() {
      MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
      MongoDatabase database = mongoClient.getDatabase("cord");
      MongoCollection<Document> collection1 = database.getCollection("papers");
    
      FindIterable<Document> findIterable = collection1.find(new Document());
      findIterable.forEach(printBlock);
    }
    

    或参考https://jira.mongodb.org/browse/DOCS-13638

    public String testLocal() {
      MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
      MongoDatabase database = mongoClient.getDatabase("cord");
      MongoCollection<Document> collection1 = database.getCollection("papers");
    
      FindIterable<Document> findIterable = collection1.find(new Document());
      findIterable.forEach(printBlock);
    }
    
    Consumer<Document> printBlock = new Consumer<Document>() {
      public void accept(final Document doc) {
        System.out.println(doc.toJson());
      };
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-09
      • 2017-11-07
      • 1970-01-01
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 2013-11-29
      • 2013-08-07
      相关资源
      最近更新 更多