我正在尝试从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/