在Mongo 3.3.0中使用QueryBuilder查询Mongo集合

编程入门 行业动态 更新时间:2024-10-10 13:19:53
本文介绍了在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集合

本文发布于:2023-11-25 21:12:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631365.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Mongo   QueryBuilder

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!