在Mongo 3.3.0中使用QueryBuilder查询Mongo集合 [英] Querying Mongo Collection using QueryBuilder in Mongo 3.3.0

查看:208
本文介绍了在Mongo 3.3.0中使用QueryBuilder查询Mongo集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用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集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆