Mongo DB Java 3.x Driver - Group By Query

后端 未结 1 1683
一个人的身影
一个人的身影 2020-12-12 05:41

I\'d like to learn how to implement group by query using Mongo DB Java 3.x Driver. I want to group my collection through the usernames

1条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 05:43

    You should try not to use old object (BasicDBObject) types with Mongo 3.x. You can try something like this.

    import static com.mongodb.client.model.Accumulators.*;
    import static com.mongodb.client.model.Aggregates.*;
    import static java.util.Arrays.asList;
    
    Bson group = group("$username", sum("tweetCount", 1));
    Bson sort = sort(new Document("tweetCount", -1));
    AggregateIterable  output = collection.aggregate(asList(group, sort));
    

    0 讨论(0)
提交回复
热议问题