我们使用mongo-java-driver 3.0.4的代码曾经像 -
DBCollection dbCollection = mongoClient.getDB(databaseName).getCollection(collectionName);
QueryBuilder queryBuilder = new QueryBuilder();
/** queryBuilder.put() for building the query */
DBCursor dbCursor = dbCollection.find(queryBuilder.get());
while(dbCursor.hasNext()) {
DBObject dbObject = dbCursor.next();
// add entries to a list of TDocument type
}
将其转换为mongo-java-driver 3.3.0,我最终得到了这个 -
MongoCollection<TDocument> collection = database.getCollection(collectionName, TDocument.class); //where TDocument is custom document class of ours
QueryBuilder queryBuilder = new QueryBuilder();
/** queryBuilder.put() for building the query */
FindIterable<TDocument> tDocumentList = collection.find(queryBuilder.get()); //this is not compiling
for (TDocument element : tDocumentList) {
names.add(element.getName()); //addition to some list of TDocument type
}
但关键是我仍然无法编译我已定义的MongoDB集合上find
操作的源代码。
这里有什么需要纠正的?我想坚持任何有助于将mongo升级到3.3.0 +的首选实现。
编辑 - 我的TDocument
类(名称与lib名称不同)类是一个简单的POJO -
public class TDocType {
private TDocType() {
}
String one;
@NotNull
String name;
String third;
String fourth;
// getter and setter for all the above
}
答案 0 :(得分:3)
投向Bson。
FindIterable<TDocument> tDocumentList = collection.find((Bson)queryBuilder.get());
更新:: 更改为使用Mongo 3.3.0中的过滤器
Bson filter = Filters.eq("field", "value");
FindIterable<TDocument> tDocumentList = collection.find(filter);
答案 1 :(得分:1)
您可以使用org.bson.Document替换查询构建器,如下所示:
Document query = new Document();
query.put(parameterName, parameterValue);
然后
FindIterable<TDocument> tDocumentList = collection.find(query); //this is not compiling