java - MongoDB Java驱动程序: distinct and iterative

标签 java mongodb iteration distinct

因为DBCollection#distinct()返回类型 List,而不是类型 DBCursor,如 DBCollection#find() 。 不幸的是,我的distinct的结果太大了。所以我需要一种好方法来循环查询的不同值。 感谢您的帮助!

最佳答案

驱动程序不提供像通过光标那样获取不同对象的设施,我们可以这样做,但从性能角度来看,我不知道哪个是正确的解决方案。

DBCollection coll = db.getCollection("your_collection");
    List<String> lst=coll.distinct("your_property");
    for(String l:lst){
        BasicDBObject query = new BasicDBObject("your_property", l);
        DBObject object=coll.findOne(query);
        System.out.println(object);
    }

关于java - MongoDB Java驱动程序: distinct and iterative,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28760747/

相关文章:

java - 如何使用 spring 和 Hibernate 生成使用 json 对象的 json 响应

java - 使用批量数据设计独立于数据源的应用程序

node.js - 在 mongodb 中 $facet 聚合后格式化数据

mongodb - 从 mongo db 中查找当前和以前的文档

python - 值错误: How to iterate through a tuple of Boolean True/False statements?

python - 将迭代转换为递归

java - 改造单值/数组

通过命令行运行时,Java 类文件无法加载 testng 类

java - 库中的 Google 广告商 ID

.net - 控制迭代循环内的递归