java - 如何使用 Java 从 MongoDB 3.4 版本获取数据?

标签 java mongodb crud

我正在尝试从java中的mongodb收集数据,我需要使用选择查询并将其放入jtextarea中。选择查询将填充组合框元素。

这是代码:

/**** 连接到 MongoDB ****/

MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");
MongoClient mClient = new MongoClient(connectionString);

/**** Get database ****/
MongoDatabase db = mClient.getDatabase("productDB");

mClient.getAddress();
/**** Get collection / table from 'productDB' ****/
MongoCollection<Document> tableCollection = db.getCollection("local");
/**** Find and display ****/
Document whereQuery = new Document();
whereQuery.put("Product Category",categoryCB.getSelectedIndex());



MongoCursor<Document> cursor = tableCollection.find(whereQuery);
mClient.close();

在tableCollection.find中显示:

cannot convert from FindIterable to MongoCursor

有什么方法可以以不同的方式做到这一点吗?

最佳答案

您正在尝试在 MongoCursor 引用中接收 FindIterable 对象。

将引用变量“cursor”的类型从 MongoCursor 更改为 FindIterable 或其父类(super class)型 MongoIterable

FindIterable<Document> cursor = tableCollection.find(whereQuery);

(或)

MongoIterable<Document> cursor = tableCollection.find(whereQuery);

关于java - 如何使用 Java 从 MongoDB 3.4 版本获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47223332/

相关文章:

javascript - Mongoose findOne 内部循环

java - 使用 Maven 预填充 MongoDB 数据库

java - 在 JTable 中显示来自数据库的数据

java - 如何在android中从arraylist中使文本的一部分加粗

java - Apache Camel 多部分 HTTP post(文件上传)

javascript - 无法通过将值字段和集合名称作为 Node js中的参数传递来从mongodb获取记录

javascript - DELETE 在 Express/Postgres 中未按预期工作

java - 在 Java 中处理 CRUD 操作等重复任务的最佳方法是什么?

java - Kinesis 工作人员错误 : Caught exception when initializing LeaseCoordinator

java - 使用 LinkedHashMap 进行迭代和分配