Query MongoDB for ordered distinct values

折月煮酒 提交于 2019-12-07 01:06:03

问题


I am using Morphia Java driver for querying a MongoDB that contains a collection of the following form:

MyCollection {
   TypeA
   TypeB
}

I want to retrieve all distinct values of TypeB which I do using the following code:

DBCollection myCol = getDatastore().getCollection(MyCollection.class);
List typeBs = myCol.distinct("TypeB");

Above code works as expected, but list of distinct values is of course not sorted.

I have experimented with the following code:

DBCollection myCol = getDatastore().getCollection(MyCollection.class);
DBObject orderBy = new BasicDBObject("$orderby", new BasicDBObject("TypeB", 1);
List typeBs = myCol.distinct("TypeB", orderBy);

But in this case the list is empty, where are my assumptions wrong?

UPDATE

By using the CLI I found out that the following query returned the expected result:

> db.mycollection.find({$query : {}, $orderby : { TypeB : 1 }})

So I adjusted my code accordingly:

DBCollection myCol = getDatastore().getCollection(MyCollection.class);

BasicDBObject ascending = new BasicDBObject("TypeB", 1);
BasicDBObject filter = new BasicDBObject();
filter.put("$query", new BasicDBObject());
filter.put("$orderby", ascending);

List typeBs = myCol.distinct("TypeB", filter);

Still result contains 0 entries!

What really makes me confused is that the same query works if I use .find instead of .distinct:

DBCollection myCol = getDatastore().getCollection(MyCollection.class);

BasicDBObject ascending = new BasicDBObject("TypeB", 1);
BasicDBObject filter = new BasicDBObject();
filter.put("$query", new BasicDBObject());
filter.put("$orderby", ascending);

myCol.find(filter).hasNext();  <-- Evaluates to TRUE

Why is it that the filter doesn't work when using the distinct method-call?


回答1:


the second DBObject parameter of DBCollection.distinct() is a query object, used to define the matching criteria. To sort the list distincts you can use:

List typeBs = myCol.distinct("TypeB");
java.util.Collections.sort(typeBs);


来源:https://stackoverflow.com/questions/12475361/query-mongodb-for-ordered-distinct-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!