Suppose each of my document is in the below format
{"_id" : ObjectId(""),"WordName":"foo","numberOfOccurances" :1}
{"_id" : ObjectId(""),"WordName":"bar","numberOfOccurances" :5}
{"_id" : ObjectId(""),"WordName":"abc","numberOfOccurances" :1}
{"_id" : ObjectId(""),"WordName":"pen","numberOfOccurances" :1}
{"_id" : ObjectId(""),"WordName":"box","numberOfOccurances" :5}
I need to get all documents having minimum value in the "numberofoccurrences" field.My expected output as below
{"_id" : ObjectId(""),"WordName":"foo","numberOfOccurances" :1}
{"_id" : ObjectId(""),"WordName":"abc","numberOfOccurances" :1}
{"_id" : ObjectId(""),"WordName":"pen","numberOfOccurances" :1}
I have tried several ways.Below is my code which will sort and it gives only one document with minimum value in "numberOfOccurences" field.
Bson sort = sort(new Document("numberOfOccurances"
+ "", 1));
Bson limit=new Document("$limit",1);
AggregateIterable<Document> output
=collection.aggregate(asList(sort,limit)).allowDiskUse(true);
How can I get all the documents having minimum value in "numberOfOccurences" field using java?
Thanks in Advance
You can try something like below. It is not efficient as everything in the collection will be grouped.
Output: