Skip to content

Commit 9f9658b

Browse files
committed
feat: add count with bson query filter
Signed-off-by: Maximillian Arruda <[email protected]>
1 parent 1ea0b28 commit 9f9658b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/MongoDBDocumentManager.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,24 @@ public Stream<DocumentEntity> select(String collectionName, Bson filter) {
229229
return stream(documents.spliterator(), false).map(MongoDBUtils::of)
230230
.map(ds -> DocumentEntity.of(collectionName, ds));
231231
}
232+
232233
private Bson getSort(Sort sort) {
233234
return sort.isAscending() ? Sorts.ascending(sort.property()) : Sorts.descending(sort.property());
234235
}
235236

237+
/**
238+
* Returns the number of documents in the collection that match the given query filter.
239+
*
240+
* @param collectionName the collection name
241+
* @param filter the query filter
242+
* @return the number of documents founded.
243+
* @throws NullPointerException when filter or collectionName is null
244+
*/
245+
public long count(String collectionName, Bson filter) {
246+
Objects.requireNonNull(filter, "filter is required");
247+
Objects.requireNonNull(collectionName, "collectionName is required");
248+
MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
249+
return collection.countDocuments(filter);
250+
}
251+
236252
}

0 commit comments

Comments
 (0)