Skip to content

Commit 9cef1d4

Browse files
committed
Create delete method using filter on mongodb driver
Signed-off-by: Otavio Santana <[email protected]>
1 parent be839c6 commit 9cef1d4

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.mongodb.client.MongoDatabase;
2121
import com.mongodb.client.model.Projections;
2222
import com.mongodb.client.model.Sorts;
23+
import com.mongodb.client.result.DeleteResult;
2324
import jakarta.nosql.Sort;
2425
import jakarta.nosql.SortType;
2526
import jakarta.nosql.document.DocumentCollectionManager;
@@ -131,6 +132,7 @@ public void delete(DocumentDeleteQuery query) {
131132
collection.deleteMany(mongoDBQuery);
132133
}
133134

135+
134136
@Override
135137
public Stream<DocumentEntity> select(DocumentQuery query) {
136138
Objects.requireNonNull(query, "query is required");
@@ -155,11 +157,29 @@ public Stream<DocumentEntity> select(DocumentQuery query) {
155157

156158
}
157159

160+
/**
161+
* Removes all documents from the collection that match the given query filter.
162+
* If no documents match, the collection is not modified.
163+
*
164+
* @param collectionName the collection name
165+
* @param filter the delete filter
166+
* @return the number of documents deleted.
167+
* @throws NullPointerException when filter or collectionName is null
168+
*/
169+
public long delete(String collectionName, Bson filter) {
170+
Objects.requireNonNull(filter, "filter is required");
171+
Objects.requireNonNull(collectionName, "collectionName is required");
172+
173+
MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
174+
DeleteResult result = collection.deleteMany(filter);
175+
return result.getDeletedCount();
176+
}
177+
158178
/**
159179
* Finds all documents in the collection.
160180
*
161181
* @param collectionName the collection name
162-
* @param filter the query filter
182+
* @param filter the query filter
163183
* @return the stream result
164184
* @throws NullPointerException when filter or collectionName is null
165185
*/

0 commit comments

Comments
 (0)