Browse Source

DATADOC-183 - Added count(…) methods to MongoOperations.

MongoTemplate is now able to count documents using either an entity class or collection name.
pull/1/head
Oliver Gierke 14 years ago
parent
commit
39bc6771b7
  1. 18
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java
  2. 41
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  3. 42
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

18
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java

@ -504,6 +504,24 @@ public interface MongoOperations {
*/ */
<T> T findAndRemove(Query query, Class<T> entityClass, String collectionName); <T> T findAndRemove(Query query, Class<T> entityClass, String collectionName);
/**
* Returns the number of documents for the given {@link Query} by querying the collection of the given entity class.
*
* @param query
* @param entityClass must not be {@literal null}.
* @return
*/
long count(Query query, Class<?> entityClass);
/**
* Returns the number of documents for the given {@link Query} querying the given collection.
*
* @param query
* @param collectionName must not be {@literal null} or empty.
* @return
*/
long count(Query query, String collectionName);
/** /**
* Insert the object into the collection for the entity type of the object to save. * Insert the object into the collection for the entity type of the object to save.
* <p/> * <p/>

41
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

@ -512,6 +512,28 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
entityClass); entityClass);
} }
public long count(Query query, Class<?> entityClass) {
Assert.notNull(entityClass);
return count(query, entityClass, determineCollectionName(entityClass));
}
public long count(final Query query, String collectionName) {
return count(query, null, collectionName);
}
private long count(Query query, Class<?> entityClass, String collectionName) {
Assert.hasText(collectionName);
final DBObject dbObject = query == null ? null : mapper.getMappedObject(query.getQueryObject(),
entityClass == null ? null : mappingContext.getPersistentEntity(entityClass));
return execute(collectionName, new CollectionCallback<Long>() {
public Long doInCollection(DBCollection collection) throws MongoException, DataAccessException {
return collection.count(dbObject);
}
});
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.MongoOperations#insert(java.lang.Object) * @see org.springframework.data.mongodb.core.MongoOperations#insert(java.lang.Object)
@ -832,8 +854,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
entityClass), collectionName); entityClass), collectionName);
} }
public <T> MapReduceResults<T> mapReduce(String inputCollectionName, String mapFunction, String reduceFunction, Class<T> entityClass) { public <T> MapReduceResults<T> mapReduce(String inputCollectionName, String mapFunction, String reduceFunction,
return mapReduce(null, inputCollectionName, mapFunction, reduceFunction, new MapReduceOptions().outputTypeInline(), entityClass); Class<T> entityClass) {
return mapReduce(null, inputCollectionName, mapFunction, reduceFunction, new MapReduceOptions().outputTypeInline(),
entityClass);
} }
public <T> MapReduceResults<T> mapReduce(String inputCollectionName, String mapFunction, String reduceFunction, public <T> MapReduceResults<T> mapReduce(String inputCollectionName, String mapFunction, String reduceFunction,
@ -841,12 +865,14 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
return mapReduce(null, inputCollectionName, mapFunction, reduceFunction, mapReduceOptions, entityClass); return mapReduce(null, inputCollectionName, mapFunction, reduceFunction, mapReduceOptions, entityClass);
} }
public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction, String reduceFunction, Class<T> entityClass) { public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction,
return mapReduce(query, inputCollectionName, mapFunction, reduceFunction, new MapReduceOptions().outputTypeInline(), entityClass); String reduceFunction, Class<T> entityClass) {
return mapReduce(query, inputCollectionName, mapFunction, reduceFunction,
new MapReduceOptions().outputTypeInline(), entityClass);
} }
public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction, String reduceFunction, public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction,
MapReduceOptions mapReduceOptions, Class<T> entityClass) { String reduceFunction, MapReduceOptions mapReduceOptions, Class<T> entityClass) {
String mapFunc = replaceWithResourceIfNecessary(mapFunction); String mapFunc = replaceWithResourceIfNecessary(mapFunction);
String reduceFunc = replaceWithResourceIfNecessary(reduceFunction); String reduceFunc = replaceWithResourceIfNecessary(reduceFunction);
DBCollection inputCollection = getCollection(inputCollectionName); DBCollection inputCollection = getCollection(inputCollectionName);
@ -872,7 +898,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
} }
String error = commandResult.getErrorMessage(); String error = commandResult.getErrorMessage();
if (error != null) { if (error != null) {
throw new InvalidDataAccessApiUsageException("Command execution failed: Error [" + error + "], Command = " + commandObject); throw new InvalidDataAccessApiUsageException("Command execution failed: Error [" + error + "], Command = "
+ commandObject);
} }
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {

42
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

@ -847,4 +847,46 @@ public class MongoTemplateTests {
assertEquals(1, names.size()); assertEquals(1, names.size());
//template.remove(new Query(), Person.class); //template.remove(new Query(), Person.class);
} }
/**
* @see DATADOC-183
*/
@Test
public void countsDocumentsCorrectly() {
assertThat(template.count(new Query(), Person.class), is(0L));
Person dave = new Person("Dave");
Person carter = new Person("Carter");
template.save(dave);
template.save(carter);
assertThat(template.count(null, Person.class), is(2L));
assertThat(template.count(query(where("firstName").is("Carter")), Person.class), is(1L));
}
/**
* @see DATADOC-183
*/
@Test(expected = IllegalArgumentException.class)
public void countRejectsNullEntityClass() {
template.count(null, (Class<?>) null);
}
/**
* @see DATADOC-183
*/
@Test(expected = IllegalArgumentException.class)
public void countRejectsEmptyCollectionName() {
template.count(null, "");
}
/**
* @see DATADOC-183
*/
@Test(expected = IllegalArgumentException.class)
public void countRejectsNullCollectionName() {
template.count(null, (String) null);
}
} }

Loading…
Cancel
Save