MongoDB
 SQL >> Datenbank >  >> NoSQL >> MongoDB

Aggregationen mit dem MongoDB Java Driver ausführen

Mit dem 3.x-Treiber:

AggregateIterable<Document> output = collection.aggregate(Arrays.asList(
  new Document("$unwind", "$views"),
  new Document("$match", new Document("views.isActive", true)),
  new Document("$sort", new Document("views.date", 1)),
  new Document("$limit", 200)
));

Mit statischem Import wird es lesbarer:

import static com.mongodb.client.model.Aggregates.*;

Der 2.x-Treiber nutzt BasicDBObject und .results(). Die 3.x-API liefert ein typisiertes AggregateIterable, das sich lazy verarbeiten lässt.