简体   繁体   中英

MongoDB Java Driver 3.0 - Static functions for aggregation

I've been using MongoDB 3.0 with Java for a while now and I never had to use aggregation until now. I'm now in need of such functionality and I'm wondering why MongoDB developers didn't add any static functions for aggregation as they did for projections, filters and sorts.

For instance, when you want to query a document where the field docId equals 1, you can use:

Document document = db.getCollection("test").find(new Document("$eq", new Document("docId", 1)).first();

Or alternatively, which is awesome (providing you've imported the model.Filters class):

Document document = db.getCollection("test").find(eq("docID",1)).first();

But when it comes to aggregation, you have only the first version available:

AggregateIterable<Document> iterable = db.getCollection("test").aggregate(asList(new Document("$match", new Document("docId", 1))));

Is there any particular reason they didn't add such static functions like match, group, sort, unwind, etc.? Or maybe am I so bad at googling that I haven't found it?

Thanks in advance for your explanations!

Actually, after looking deeper, it happens that they just have developed these static functions and they are available in the current version (3.1-SNAPSHOT) of MongoDB Java Driver in the classes com.mongodb.client.model.Aggregates and com.mongodb.client.model.Accumulators .

It is not said yet when the 3.1 release is due/planned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM