1
Bson group = group("$NAME",sum("amount", "$amount"));

        AggregateIterable <Document> output = collection.aggregate(asList(group));

        MongoCursor<org.bson.Document> cursor = output.iterator();
        while(cursor.hasNext()){ 
            System.out.println(cursor.next());

        }
    }

The above code returning results in reverse order

svm
  • 25
  • 1
  • 9
  • 1
    If you haven't [specified an order yourself](https://docs.mongodb.com/manual/reference/operator/aggregation/sort/), then [you might get the results back in any order](http://stackoverflow.com/a/11599283/174843). – Vince Bowdren Mar 17 '17 at 12:16
  • yeah,It worked by sorting both fields i.e. Bson sort = sort(new Document("_id", 1).append("amount", -1)); – svm Mar 17 '17 at 15:47
  • Can you have a look at the question [link](http://stackoverflow.com/q/42862170/7720299) @http://stackoverflow.com/users/174843/vince-bowdren – svm Mar 18 '17 at 07:30
  • Possible duplicate of [How does MongoDB sort records when no sort order is specified?](http://stackoverflow.com/questions/11599069/how-does-mongodb-sort-records-when-no-sort-order-is-specified) – Vince Bowdren Mar 18 '17 at 17:32
  • I want to retrieve all the columns of a collection, I want to perform select * from tablename group by Name). Can you please go through the link in the above comment and let me know how to do? @ http://stackoverflow.com/users/174843/vince-bowdren – svm Mar 18 '17 at 17:57

0 Answers0