Skip to content

Commit e2ad27a

Browse files
committed
Create aggregate method
Signed-off-by: Otavio Santana <[email protected]>
1 parent ac09e29 commit e2ad27a

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

mongodb-driver/src/main/java/org/eclipse/jnosql/communication/mongodb/document/MongoDBDocumentCollectionManager.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package org.eclipse.jnosql.communication.mongodb.document;
1717

18+
import com.mongodb.client.AggregateIterable;
1819
import com.mongodb.client.FindIterable;
1920
import com.mongodb.client.MongoCollection;
2021
import com.mongodb.client.MongoDatabase;
@@ -33,6 +34,9 @@
3334
import org.eclipse.jnosql.communication.document.Documents;
3435

3536
import java.time.Duration;
37+
import java.util.Collections;
38+
import java.util.List;
39+
import java.util.Map;
3640
import java.util.Objects;
3741
import java.util.stream.Stream;
3842
import java.util.stream.StreamSupport;
@@ -175,8 +179,24 @@ public long delete(String collectionName, Bson filter) {
175179
return result.getDeletedCount();
176180
}
177181

178-
public long aggregate(String collectionName, Bson filter) {
179-
return 0L;
182+
/**
183+
* Aggregates documents according to the specified aggregation pipeline.
184+
*
185+
* @param collectionName the collection name
186+
* @param pipeline the aggregation pipeline
187+
* @return the number of documents deleted.
188+
* @throws NullPointerException when filter or collectionName is null
189+
*/
190+
public Map<String, Object> aggregate(String collectionName, List<Bson> pipeline) {
191+
Objects.requireNonNull(pipeline, "filter is required");
192+
Objects.requireNonNull(collectionName, "collectionName is required");
193+
MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
194+
AggregateIterable<Document> aggregate = collection.aggregate(pipeline);
195+
for (Document document : aggregate) {
196+
BsonDocument bson = document.toBsonDocument();
197+
System.out.println(bson);
198+
}
199+
return Collections.emptyMap();
180200
}
181201

182202
/**

0 commit comments

Comments
 (0)