【发布时间】:2015-07-12 15:23:48
【问题描述】:
如何使用 Java MongoDB 3.0 驱动程序查询distinct?
我正在尝试从 MongoDB 中的 locations 集合中查询唯一的 categories 记录。在 Mongo shell 中,这很简单:db.locations.distinct("categories");
在 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();
【问题讨论】:
-
我无法让
distinct工作(无论我做什么都会返回一个空的迭代)。这对您有用吗?如接受的答案中所述? -
@Stav 是的,它对我有用。请参阅下面的我的新答案,其中包含所有步骤。
标签: java mongodb-query