我们使用mongo-java-driver 3.0.4编写的代码以前就像-
Our code with mongo-java-driver 3.0.4 used to be like -
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后,我最终得到了它-
Converting this to the mongo-java-driver 3.3.0, I ended up with this -
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操作编译源代码.
But the point is I am still not able to compile the source for the find operation on the MongoDB collection that I have defined.
这里需要纠正什么?我想坚持使用任何有助于将mongo升级到3.3.0+的首选实现.
What needs to be corrected here? I would want to stick to any preferred implementation that helps in upgrading mongo to 3.3.0+.
编辑 -我的TDocument类(在下面与库名称不同的命名)是一个简单的POJO,如-
Edit - My TDocument class (named differently below from the lib name) class is a simple POJO as -
public class TDocType { private TDocType() { } String one; @NotNull String name; String third; String fourth; // getter and setter for all the above }推荐答案
投射到Bson.
FindIterable<TDocument> tDocumentList = collection.find((Bson)queryBuilder.get());更新:: 更改为使用Mongo 3.3.0中的过滤器
Update:: Change to use the filters from Mongo 3.3.0
Bson filter = Filters.eq("field", "value"); FindIterable<TDocument> tDocumentList = collection.find(filter);更多推荐
在Mongo 3.3.0中使用QueryBuilder查询Mongo集合
发布评论