Doctrine ODM Distinct With Limit - mongodb

I have the following problem: I can't limit number of results when using distinct. Exemple :
$stores = $this->dm->createQueryBuilder('Application\Document\Item')
->distinct('storeName')
->limit(10)
->getQuery()
->execute();
This query render 100 entries but I want only 10 results.

With query builder class in ORM you need to use:
->setMaxResults(10);
As per #Siol and #john Smith said, in ODM you could use limit:
->limit(10);

I don't think distinct will work with limit as suggested in the Jira mongodb issue ticket Ability to use Limit() with Distinct():
The current Distinct() implementation only allows for bringing back
ALL distinct values in the collection or matching a query, but there
is no way to limit these results. This would be very convenient and
there are many use cases.

Related

How to use distinct with skip and limit in spring data mongodb

I have a requirement where i need to use distinct with skip and limit and i have googled a lot for this but found nothing useful and as per some answers it is not supported with old version spring data mongodb and in the newer version is there any way to use this or is there any solution to achieve distinct with skip and limit with aggregation framework
I have not added distinct but used skip, limit with aggregation
Aggregation agg = newAggregation(match(Criteria.where("goalId").is(goal.getId())), skip((long) request.getPage() * request.getSize()),limit(request.getSize()));
final AggregationResults<ActivityHistory> results = mongoOperations.aggregate(agg, ActivityHistory.class, ActivityHistory.class);
List<ActivityHistory> activityHistoryList = results.getMappedResults();

Hazelcast- Distributed query aggregations with group by support

We need to query IMDG example using Hazelcast 3.8-EA version
select sum(salary),sum(bonus),dept from Employee where birthYear > 1989 group by dept
where clause :: SqlPredicate("birthYear > 1989")
Aggregation::
Using Aggregators.doubleSum("salary") , Aggregators.doubleSum("bonus") on Employee Map
Or by extending AbstractAggregator
Question is how to handle multiple aggregation using built-in aggregations and how to handle group by clause?
There's no official group by support yet but what you could do is to create your own SumWithGroupBy aggregation that sums the salary and bonus per group in the way you want it to be grouped.
You can have a look at the Aggregators.doubleSum code to see how the aggregation can be implemented.
It's a bit of manual coding but it will be just a couple of lines of custom logic.

How do I set `skip` (offset) for reactivemongo mongodb driver?

I need to skip a number of documents (offset) from a query, and only return limit number of documents that go after. I know the following naive approach:
collection.find(BSONDocument())
.cursor[T].collect[List](offset+limit).map(_.drop(offset))
but it is not really desired because it will load offset+limit number of documents in JVM memory, whereas I'd like to filter them on the "database" side.
Solution: use QueryOpts. Example:
collection.find(BSONDocument())
.options(QueryOpts(skipN = offset))
.cursor[T].collect[List](limit)
Note that using skip is not very efficient because mongodb does not support effective pagination, it will just skip the desired number by iterating through all the documents.
VasyaNovikov answer is certainly correct. Reactive mongo offers a more intuitive API:
collection.find(BSONDocument())
.skip(offset)
.cursor[T]
.collect[List](limit, Cursor.FailOnError[List[T]]())

How to retrieve whole document in mongodb distinct query rather than only the key?

Mongodb distinct command returns the list of distinct keys in a given document.
Is there any way to retrieve the whole document rather than only keys using mongodb java driver or morphia?
In the documentation it says:
When possible, the distinct command will use an index to find the documents in the query as well as to return the data.
How does this work, when I try using java driver it always returns the list of keys to my queries. And it doesn't take limit and order queries into consideration. Is it possible to give a order by and limit queries into distinct query?
Thanks for the feedback.

pymongo sort grouped results

I need to group and sort by date_published some documents stored on mongodb using pymongo.
the group part went just fine :) but when I'm addding .sort() to the query it keeps failing no matter what I tried :(
here is my query:
db.activities.group(keyf_code,cond,{},reduce_code)
I want to sort by a field called "published" (timestamp)
tried to do
db.activities.group(keyf_code,cond,{},reduce_code).sort({"published": -1})
and many more variations without any success
ideas anyone?
You can't currently do sort with group in MongoDB. You can use MapReduce instead which does support a sort option. There is also an enhancement request to support group with sort here.
Although MongoDB doesn't do what you want, you can always use Python to do the sorting:
result = db.activities.group(keyf_code,cond,{},reduce_code)
result = sorted(result, key=itemgetter("published"), reverse=True)