Skip to content

Commit 91fdf59

Browse files
authored
Merge pull request #228 from dearrudam/mongodb-count-by-filter
Adding to JNoSQL MongoDB Database Implementation a count method by Bson query filter
2 parents 16d2e03 + c913f1a commit 91fdf59

File tree

6 files changed

+129
-19
lines changed

6 files changed

+129
-19
lines changed

CHANGELOG.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
1616
- Update package name convention to `org.jnosql.databases.[DATABASE].[LAYER]`
1717
- Integrate the mapping layer on this repository
1818
- Upgrade the AWS SDK for DynamoDB to version 2.20.65;
19+
- Added to the JNoSQL MongoDB Database Implementation the MongoDB Aggregation support;
20+
- Added into the JNoSQL MongoDB Database Implementation a count method by Bson query filter
1921

2022
=== Fixed
2123

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
}

jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/mapping/DefaultMongoDBTemplate.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,24 @@ class DefaultMongoDBTemplate extends AbstractDocumentTemplate implements MongoDB
4242

4343
private final Instance<MongoDBDocumentManager> manager;
4444

45-
private final DocumentEntityConverter converter;
45+
private final DocumentEntityConverter converter;
4646

47-
private final DocumentWorkflow workflow;
47+
private final DocumentWorkflow workflow;
4848

49-
private final EntitiesMetadata entities;
49+
private final EntitiesMetadata entities;
5050

51-
private final Converters converters;
51+
private final Converters converters;
5252

53-
private final DocumentEventPersistManager persistManager;
53+
private final DocumentEventPersistManager persistManager;
5454

5555

5656
@Inject
5757
DefaultMongoDBTemplate(Instance<MongoDBDocumentManager> manager,
58-
DocumentEntityConverter converter,
59-
DocumentWorkflow workflow,
60-
EntitiesMetadata entities,
61-
Converters converters,
62-
DocumentEventPersistManager persistManager) {
58+
DocumentEntityConverter converter,
59+
DocumentWorkflow workflow,
60+
EntitiesMetadata entities,
61+
Converters converters,
62+
DocumentEventPersistManager persistManager) {
6363
this.manager = manager;
6464
this.converter = converter;
6565
this.workflow = workflow;
@@ -145,4 +145,19 @@ public <T> Stream<Map<String, BsonValue>> aggregate(Class<T> entity, List<Bson>
145145
return this.getManager().aggregate(entityMetadata.name(), pipeline);
146146
}
147147

148+
@Override
149+
public long count(String collectionName, Bson filter) {
150+
Objects.requireNonNull(collectionName, "collection name is required");
151+
Objects.requireNonNull(filter, "filter is required");
152+
return this.getManager().count(collectionName, filter);
153+
}
154+
155+
@Override
156+
public <T> long count(Class<T> entity, Bson filter) {
157+
Objects.requireNonNull(entity, "entity is required");
158+
Objects.requireNonNull(filter, "filter is required");
159+
EntityMetadata entityMetadata = this.entities.get(entity);
160+
return this.getManager().count(entityMetadata.name(), filter);
161+
}
162+
148163
}

jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/mapping/MongoDBTemplate.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,31 @@ public interface MongoDBTemplate extends JNoSQLDocumentTemplate {
8888
/**
8989
* Aggregates documents according to the specified aggregation pipeline.
9090
*
91-
* @param entity the collection name
92-
* @param <T> the entity type
93-
* @param pipeline the aggregation pipeline
91+
* @param entity the collection name
92+
* @param <T> the entity type
93+
* @param pipeline the aggregation pipeline
9494
* @return the number of documents deleted.
9595
* @throws NullPointerException when filter or entity is null
9696
*/
9797
<T> Stream<Map<String, BsonValue>> aggregate(Class<T> entity, List<Bson> pipeline);
98-
98+
99+
/**
100+
* Returns the number of items in the collection that match the given query filter.
101+
*
102+
* @param collectionName the collection name
103+
* @param filter the query
104+
* @return the number of documents founded.
105+
* @throws NullPointerException when filter or collectionName is null
106+
*/
107+
long count(String collectionName, Bson filter);
108+
109+
/**
110+
* Returns the number of items in the collection that match the given query filter.
111+
*
112+
* @param entity the entity type
113+
* @param filter the filter
114+
* @return the number of documents founded.
115+
* @throws NullPointerException when filter or collectionName is null
116+
*/
117+
<T> long count(Class<T> entity, Bson filter);
99118
}

jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/communication/MongoDBSpecificFeaturesTest.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.stream.Collectors;
3838
import java.util.stream.Stream;
3939

40+
import static com.mongodb.client.model.Filters.and;
4041
import static com.mongodb.client.model.Filters.eq;
4142
import static org.assertj.core.api.Assertions.assertThat;
4243
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
@@ -60,14 +61,14 @@ public void beforeEach() {
6061
}
6162

6263
@Test
63-
public void shouldReturnErrorOnSelectWhenThereIsNullParameter(){
64+
public void shouldReturnErrorOnSelectWhenThereIsNullParameter() {
6465
Assertions.assertThrows(NullPointerException.class,
6566
() -> entityManager.select(null, null));
6667
Assertions.assertThrows(NullPointerException.class,
6768
() -> entityManager.select(COLLECTION_NAME, null));
6869

6970
Assertions.assertThrows(NullPointerException.class,
70-
() -> entityManager.select(null, eq("name", "Poliana")));
71+
() -> entityManager.select(null, eq("name", "Poliana")));
7172
}
7273

7374
@Test
@@ -81,14 +82,14 @@ public void shouldFindDocument() {
8182
}
8283

8384
@Test
84-
public void shouldReturnErrorOnDeleteWhenThereIsNullParameter(){
85+
public void shouldReturnErrorOnDeleteWhenThereIsNullParameter() {
8586
Assertions.assertThrows(NullPointerException.class,
8687
() -> entityManager.delete(null, null));
8788
Assertions.assertThrows(NullPointerException.class,
8889
() -> entityManager.delete(COLLECTION_NAME, null));
8990

9091
Assertions.assertThrows(NullPointerException.class,
91-
() -> entityManager.delete(null, eq("name", "Poliana")));
92+
() -> entityManager.delete(null, eq("name", "Poliana")));
9293
}
9394

9495
@Test
@@ -105,7 +106,7 @@ public void shouldDelete() {
105106
}
106107

107108
@Test
108-
public void shouldReturnErrorOnAggregateWhenThereIsNullParameter(){
109+
public void shouldReturnErrorOnAggregateWhenThereIsNullParameter() {
109110
Assertions.assertThrows(NullPointerException.class,
110111
() -> entityManager.aggregate(null, null));
111112
Assertions.assertThrows(NullPointerException.class,
@@ -146,4 +147,32 @@ private DocumentEntity getEntity() {
146147
return entity;
147148
}
148149

150+
@Test
151+
public void shouldCountWithFilter() {
152+
153+
entityManager.insert(getEntity());
154+
var filter = eq("name", "Poliana");
155+
Assertions.assertEquals(1L, entityManager.count(COLLECTION_NAME, filter));
156+
157+
var filter2 = and(filter,eq("city", "Salvador"));
158+
Assertions.assertEquals(1L, entityManager.count(COLLECTION_NAME, filter2));
159+
160+
var filter3 = and(filter,eq("city", "São Paulo"));
161+
Assertions.assertEquals(0L, entityManager.count(COLLECTION_NAME, filter3));
162+
163+
}
164+
165+
@Test
166+
public void shouldReturnErrorOnCountWithInvalidFilter() {
167+
168+
Assertions.assertThrows(NullPointerException.class,
169+
() -> entityManager.count(null, null));
170+
Assertions.assertThrows(NullPointerException.class,
171+
() -> entityManager.count(COLLECTION_NAME, null));
172+
Assertions.assertThrows(NullPointerException.class,
173+
() -> entityManager.count(null, eq("name","Poliana")));
174+
175+
}
176+
177+
149178
}

jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/mapping/DefaultMongoDBTemplateTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,33 @@ public void shouldAggregateWithEntity() {
192192
Mockito.verify(manager).aggregate("Person", predicates);
193193
}
194194

195+
@Test
196+
public void shouldCountByFilterWithCollectionName() {
197+
var filter = eq("name", "Poliana");
198+
199+
template.count("Person", filter);
200+
201+
Mockito.verify(manager).count("Person", filter);
202+
}
203+
204+
@Test
205+
public void shouldCountByFilterWithEntity() {
206+
var filter = eq("name", "Poliana");
207+
208+
template.count(Person.class, filter);
209+
210+
Mockito.verify(manager).count("Person", filter);
211+
}
212+
213+
@Test
214+
public void shouldReturnErrorOnCountByFilterMethod() {
215+
var filter = eq("name", "Poliana");
216+
assertThrows(NullPointerException.class, () -> template.count((String) null, null));
217+
assertThrows(NullPointerException.class, () -> template.count((String) null, filter));
218+
assertThrows(NullPointerException.class, () -> template.count("Person", null));
219+
assertThrows(NullPointerException.class, () -> template.count((Class<Person>) null, null));
220+
assertThrows(NullPointerException.class, () -> template.count((Class<Person>) null, filter));
221+
assertThrows(NullPointerException.class, () -> template.count(Person.class, null));
222+
}
223+
195224
}

0 commit comments

Comments
 (0)