没有过滤器的 Java MongoDB 3.0 驱动程序查询不同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30009847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:17:52  来源:igfitidea点击:

Java MongoDB 3.0 driver query distinct without filter

javamongodb-query

提问by ThisClark

How may I query distinctwith the Java MongoDB 3.0 driver?

如何使用 Java MongoDB 3.0 驱动程序查询不同

I am attempting to query unique categoriesrecords from a locationscollection in MongoDB. In the Mongo shell, this is very simple: db.locations.distinct("categories");

我正在尝试从MongoDB 中的位置集合查询唯一类别记录。在 Mongo shell 中,这很简单:db.locations.distinct("categories");

In Java, it's not the same.

在 Java 中,情况不一样。

MongoClient client = new MongoClient();
MongoDatabase db = client.getDatabase("yelp");

//this will not compile, although examples from before 3.0 do it this way
MongoCursor<Document> c = 
    db.getCollection("locations").distinct("categories").iterator();

回答by jyemin

To let you avoid casts for distinct, the MongoCollection API lets you provide the expected type of the distinct values for the field. So if you know they are all strings, for example, you can write:

为了让您避免对 distinct 进行强制转换,MongoCollection API 允许您为字段提供不同值的预期类型。因此,例如,如果您知道它们都是字符串,则可以编写:

MongoCursor<String> c = 
   db.getCollection("locations").distinct("categories", String.class).iterator();

or all numbers:

或所有数字:

MongoCursor<Number> c = 
   db.getCollection("locations").distinct("categories", Number.class).iterator();

You can still do:

你仍然可以这样做:

MongoCursor<Object> c = 
   db.getCollection("locations").distinct("categories", Object.class).iterator();

if you can't guarantee anything about the types of the values for the field you're querying.

如果您不能保证您查询的字段的值的类型。

回答by ThisClark

I'll create an example database in the MongoDB shell and go as far as delivering a distinct query in Java with the 3.0 driver.

我将在 MongoDB shell 中创建一个示例数据库,并使用 3.0 驱动程序在 Java 中提供不同的查询。

In the shell, create a database with demo data:

在 shell 中,创建一个包含演示数据的数据库:

use imagedb;
db.createCollection("image_files");
db.image_files.insert({ file: "1.jpg", aspect: "4:3" });
db.image_files.insert({ file: "1.jpg", aspect: "16:9" });
db.image_files.insert({ file: "2.jpg", aspect: "4:3" });
db.image_files.insert({ file: "2.jpg", aspect: "16:9" });
db.image_files.insert({ file: "3.jpg", aspect: "2.35:1" });

In the shell, we may find distinct records.

在 shell 中,我们可能会发现不同的记录。

> db.image_files.distinct('file');
[ "1.jpg", "2.jpg", "3.jpg" ]

How to do it with Java and the MongoDB 3.0 driver?

如何使用 Java 和 MongoDB 3.0 驱动程序做到这一点?

import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;

public class Main {
  public static void main(String[] args) {
    MongoClient mongo = new MongoClient();
    MongoDatabase db = mongo.getDatabase("imagedb");
    MongoCollection<Document> filesCollection = db.getCollection("image_files");
    MongoCursor<String> files = filesCollection.distinct("file", String.class).iterator();
    while(files.hasNext()) {
      System.out.println(files.next());
    }
  }
}