Browse Source

renamed query methods to find; changed query methods taking JSON to accept the new Query object instead; added support for fields specification; removed write concern methods for collection and DB;

pull/1/head
Thomas Risberg 15 years ago
parent
commit
e2146bf997
  1. 233
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java
  2. 123
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java
  3. 4
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java
  4. 6
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java
  5. 2
      spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java
  6. 86
      spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateWriteConcernTests.java
  7. 3
      spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java

233
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java

@ -18,6 +18,8 @@ package org.springframework.data.document.mongodb; @@ -18,6 +18,8 @@ package org.springframework.data.document.mongodb;
import java.util.List;
import java.util.Set;
import org.springframework.data.document.mongodb.builder.Query;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.WriteConcern;
@ -44,32 +46,6 @@ public interface MongoOperations { @@ -44,32 +46,6 @@ public interface MongoOperations {
* @return The default collection used by this template
*/
DBCollection getDefaultCollection();
/**
* Set the {@link com.mongodb.WriteConcern} to be used for the Database.
* @param writeConcern
*/
void setDatabaseWriteConcern(WriteConcern writeConcern);
/**
* Get the {@link com.mongodb.WriteConcern} currently used by the Database.
* @return the WriteConcern
*/
WriteConcern getDatabaseWriteConcern();
/**
* Set the {@link com.mongodb.WriteConcern} to be used for the Collection.
* @param collectionName
* @param writeConcern
*/
void setCollectionWriteConcern(String collectionName, WriteConcern writeConcern);
/**
* Get the {@link com.mongodb.WriteConcern} currently used by the Collection.
* @param collectionName
* @return the WriteConcern
*/
WriteConcern getCollectionWriteConcern(String collectionName);
/**
* Execute the a MongoDB command expressed as a JSON string. This will call the method
@ -406,7 +382,6 @@ public interface MongoOperations { @@ -406,7 +382,6 @@ public interface MongoOperations {
* Query for a list of objects of type T from the specified collection, mapping the DBObject using
* the provided MongoReader.
*
*
* @param collectionName name of the collection to retrieve the objects from
* @param targetClass the parameterized type of the returned list.
* @param reader the MongoReader to convert from DBObject to an object.
@ -415,15 +390,75 @@ public interface MongoOperations { @@ -415,15 +390,75 @@ public interface MongoOperations {
<T> List<T> getCollection(String collectionName, Class<T> targetClass,
MongoReader<T> reader);
<T> List<T> queryUsingJavaScript(String query, Class<T> targetClass);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link QuerySpec}.
*
* @param query the query class that specifies the criteria used to find a record and also an optional fields specification
* @param targetClass the parameterized type of the returned list.
* @return the List of converted objects
*/
<T> List<T> query(Query query, Class<T> targetClass);
<T> List<T> queryUsingJavaScript(String query, Class<T> targetClass,
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link QuerySpec}.
*
* @param query the query class that specifies the criteria used to find a record and also an optional fields specification
* @param targetClass the parameterized type of the returned list.
* @param reader the MongoReader to convert from DBObject to an object.
* @return the List of converted objects
*/
<T> List<T> query(Query query, Class<T> targetClass,
MongoReader<T> reader);
<T> List<T> queryUsingJavaScript(String collectionName, String query,
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link QuerySpec}.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query class that specifies the criteria used to find a record and also an optional fields specification
* @param targetClass the parameterized type of the returned list.
* @return the List of converted objects
*/
<T> List<T> query(String collectionName, Query query,
Class<T> targetClass);
<T> List<T> queryUsingJavaScript(String collectionName, String query,
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link QuerySpec}.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query class that specifies the criteria used to find a record and also an optional fields specification
* @param targetClass the parameterized type of the returned list.
* @param reader the MongoReader to convert from DBObject to an object.
* @return the List of converted objects
*/
<T> List<T> query(String collectionName, Query query,
Class<T> targetClass, MongoReader<T> reader);
/**
@ -433,8 +468,108 @@ public interface MongoOperations { @@ -433,8 +468,108 @@ public interface MongoOperations {
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to
* more easily create a query document.
* The query document is specified as a standard DBObject.
*
* NOTE: A generic criteria API will be introduced in a future release. You can see the
* <a href="https://github.com/grails/inconsequential">inconsequential</a> project for an example of how that
* may look.
*
* @param query the query document that specifies the criteria used to find a record
* @param targetClass the parameterized type of the returned list.
* @return the List of converted objects
*/
<T> List<T> find(DBObject query, Class<T> targetClass);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject.
*
* @param query the query document that specifies the criteria used to find a record
* @param targetClass the parameterized type of the returned list.
* @param preparer allows for customization of the DBCursor used when iterating over the result set,
* (apply limits, skips and so on).
* @return the List of converted objects.
*/
<T> List<T> find(DBObject query, Class<T> targetClass,
CursorPreparer preparer);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader
*
* The query document is specified as a standard DBObject.
*
* @param query the query document that specifies the criteria used to find a record
* @param targetClass the parameterized type of the returned list.
* @param reader the MongoReader to convert from DBObject to an object.
* @return the List of converted objects.
*/
<T> List<T> find(DBObject query, Class<T> targetClass,
MongoReader<T> reader);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject.
*
* NOTE: A generic criteria API will be introduced in a future release. You can see the
* <a href="https://github.com/grails/inconsequential">inconsequential</a> project for an example of how that
* may look.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param targetClass the parameterized type of the returned list.
* @return the List of converted objects
*/
<T> List<T> find(String collectionName, DBObject query, Class<T> targetClass);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param targetClass the parameterized type of the returned list.
* @param preparer allows for customization of the DBCursor used when iterating over the result set,
* (apply limits, skips and so on).
* @return the List of converted objects.
*/
<T> List<T> find(String collectionName, DBObject query, Class<T> targetClass, CursorPreparer preparer);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader
*
* The query document is specified as a standard DBObject.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param targetClass the parameterized type of the returned list.
* @param reader the MongoReader to convert from DBObject to an object.
* @return the List of converted objects.
*/
<T> List<T> find(String collectionName, DBObject query, Class<T> targetClass, MongoReader<T> reader);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject and so is the fields specification.
*
* NOTE: A generic criteria API will be introduced in a future release. You can see the
* <a href="https://github.com/grails/inconsequential">inconsequential</a> project for an example of how that
@ -444,7 +579,7 @@ public interface MongoOperations { @@ -444,7 +579,7 @@ public interface MongoOperations {
* @param targetClass the parameterized type of the returned list.
* @return the List of converted objects
*/
<T> List<T> query(DBObject query, Class<T> targetClass);
<T> List<T> find(DBObject query, DBObject fields, Class<T> targetClass);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
@ -453,30 +588,30 @@ public interface MongoOperations { @@ -453,30 +588,30 @@ public interface MongoOperations {
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to
* more easily create a query document.
* The query document is specified as a standard DBObject and so is the fields specification.
*
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
* @param targetClass the parameterized type of the returned list.
* @param preparer allows for customization of the DBCursor used when iterating over the result set,
* (apply limits, skips and so on).
* @return the List of converted objects.
*/
<T> List<T> query(DBObject query, Class<T> targetClass,
<T> List<T> find(DBObject query, DBObject fields, Class<T> targetClass,
CursorPreparer preparer);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader
*
* The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to
* more easily create a query document.
* The query document is specified as a standard DBObject and so is the fields specification.
*
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
* @param targetClass the parameterized type of the returned list.
* @param reader the MongoReader to convert from DBObject to an object.
* @return the List of converted objects.
*/
<T> List<T> query(DBObject query, Class<T> targetClass,
<T> List<T> find(DBObject query, DBObject fields, Class<T> targetClass,
MongoReader<T> reader);
/**
@ -486,8 +621,7 @@ public interface MongoOperations { @@ -486,8 +621,7 @@ public interface MongoOperations {
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to
* more easily create a query document.
* The query document is specified as a standard DBObject and so is the fields specification.
*
* NOTE: A generic criteria API will be introduced in a future release. You can see the
* <a href="https://github.com/grails/inconsequential">inconsequential</a> project for an example of how that
@ -495,10 +629,11 @@ public interface MongoOperations { @@ -495,10 +629,11 @@ public interface MongoOperations {
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
* @param targetClass the parameterized type of the returned list.
* @return the List of converted objects
*/
<T> List<T> query(String collectionName, DBObject query, Class<T> targetClass);
<T> List<T> find(String collectionName, DBObject fields, DBObject query, Class<T> targetClass);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
@ -507,29 +642,29 @@ public interface MongoOperations { @@ -507,29 +642,29 @@ public interface MongoOperations {
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to
* more easily create a query document.
* The query document is specified as a standard DBObject and so is the fields specification.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
* @param targetClass the parameterized type of the returned list.
* @param preparer allows for customization of the DBCursor used when iterating over the result set,
* (apply limits, skips and so on).
* @return the List of converted objects.
*/
<T> List<T> query(String collectionName, DBObject query, Class<T> targetClass, CursorPreparer preparer);
<T> List<T> find(String collectionName, DBObject fields, DBObject query, Class<T> targetClass, CursorPreparer preparer);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader
*
* The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to
* more easily create a query document.
* The query document is specified as a standard DBObject and so is the fields specification.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
* @param targetClass the parameterized type of the returned list.
* @param reader the MongoReader to convert from DBObject to an object.
* @return the List of converted objects.
*/
<T> List<T> query(String collectionName, DBObject query, Class<T> targetClass, MongoReader<T> reader);
<T> List<T> find(String collectionName, DBObject fields, DBObject query, Class<T> targetClass, MongoReader<T> reader);
}

123
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java

@ -28,6 +28,7 @@ import org.springframework.beans.factory.InitializingBean; @@ -28,6 +28,7 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
import org.springframework.data.document.mongodb.builder.Query;
import org.springframework.jca.cci.core.ConnectionCallback;
import org.springframework.util.Assert;
@ -211,23 +212,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations { @@ -211,23 +212,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
});
}
// TODO:
public void setDatabaseWriteConcern(WriteConcern writeConcern) {
getDb().setWriteConcern(writeConcern);
}
public WriteConcern getDatabaseWriteConcern() {
return getDb().getWriteConcern();
}
public void setCollectionWriteConcern(String collectionName, WriteConcern writeConcern) {
getCollection(collectionName).setWriteConcern(writeConcern);
}
public WriteConcern getCollectionWriteConcern(String collectionName) {
return getCollection(collectionName).getWriteConcern();
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#executeCommand(java.lang.String)
*/
@ -683,75 +667,119 @@ public class MongoTemplate implements InitializingBean, MongoOperations { @@ -683,75 +667,119 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.Class)
*/
public <T> List<T> queryUsingJavaScript(String query, Class<T> targetClass) {
return query(getDefaultCollectionName(), (DBObject)JSON.parse(query), targetClass); //
public <T> List<T> query(Query query, Class<T> targetClass) {
return query(getDefaultCollectionName(), query, targetClass); //
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
*/
public <T> List<T> queryUsingJavaScript(String query, Class<T> targetClass, MongoReader<T> reader) {
return query(getDefaultCollectionName(), (DBObject)JSON.parse(query), targetClass, reader);
public <T> List<T> query(Query query, Class<T> targetClass, MongoReader<T> reader) {
return query(getDefaultCollectionName(), query, targetClass, reader);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.String, java.lang.Class)
*/
public <T> List<T> queryUsingJavaScript(String collectionName, String query, Class<T> targetClass) {
return query(collectionName, (DBObject)JSON.parse(query), targetClass); //
public <T> List<T> query(String collectionName, Query query, Class<T> targetClass) {
return find(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass); //
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.String, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
*/
public <T> List<T> queryUsingJavaScript(String collectionName, String query, Class<T> targetClass, MongoReader<T> reader) {
return query(collectionName, (DBObject)JSON.parse(query), targetClass, reader);
public <T> List<T> query(String collectionName, Query query, Class<T> targetClass, MongoReader<T> reader) {
return find(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, reader);
}
// Queries that take DBObject to express the query
// Find methods that take DBObject to express the query
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class)
*/
public <T> List<T> query(DBObject query, Class<T> targetClass) {
return query(getDefaultCollectionName(), query, targetClass); //
public <T> List<T> find(DBObject query, Class<T> targetClass) {
return find(query, null, targetClass); //
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer)
*/
public <T> List<T> query(DBObject query, Class<T> targetClass, CursorPreparer preparer) {
return query(getDefaultCollectionName(), query, targetClass, preparer); //
public <T> List<T> find(DBObject query, Class<T> targetClass, CursorPreparer preparer) {
return find(query, null, targetClass, preparer); //
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
*/
public <T> List<T> query(DBObject query, Class<T> targetClass, MongoReader<T> reader) {
return query(getDefaultCollectionName(), query, targetClass, reader);
public <T> List<T> find(DBObject query, Class<T> targetClass, MongoReader<T> reader) {
return find(query, null, targetClass, reader);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class)
*/
public <T> List<T> query(String collectionName, DBObject query, Class<T> targetClass) {
return query(collectionName, query, targetClass, (CursorPreparer) null);
public <T> List<T> find(String collectionName, DBObject query, Class<T> targetClass) {
return find(collectionName, query, null, targetClass);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer)
*/
public <T> List<T> query(String collectionName, DBObject query, Class<T> targetClass, CursorPreparer preparer) {
return executeEach(new FindCallback(query), preparer, new ReadDbObjectCallback<T>(mongoConverter, targetClass),
public <T> List<T> find(String collectionName, DBObject query, Class<T> targetClass, CursorPreparer preparer) {
return find(collectionName, query, null, targetClass, preparer);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
*/
public <T> List<T> find(String collectionName, DBObject query, Class<T> targetClass, MongoReader<T> reader) {
return find(collectionName, query, null, targetClass, reader);
}
// Find methods that take DBObject to express the query and a DBObject to express the fields specification
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class)
*/
public <T> List<T> find(DBObject query, DBObject fields, Class<T> targetClass) {
return find(getDefaultCollectionName(), query, fields, targetClass); //
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer)
*/
public <T> List<T> find(DBObject query, DBObject fields, Class<T> targetClass, CursorPreparer preparer) {
return find(getDefaultCollectionName(), query, fields, targetClass, preparer); //
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
*/
public <T> List<T> find(DBObject query, DBObject fields, Class<T> targetClass, MongoReader<T> reader) {
return find(getDefaultCollectionName(), query, fields, targetClass, reader);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class)
*/
public <T> List<T> find(String collectionName, DBObject query, DBObject fields, Class<T> targetClass) {
return find(collectionName, query, fields, targetClass, (CursorPreparer) null);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer)
*/
public <T> List<T> find(String collectionName, DBObject query, DBObject fields, Class<T> targetClass, CursorPreparer preparer) {
return executeEach(new FindCallback(query, fields), preparer, new ReadDbObjectCallback<T>(mongoConverter, targetClass),
collectionName);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
*/
public <T> List<T> query(String collectionName, DBObject query, Class<T> targetClass, MongoReader<T> reader) {
return executeEach(new FindCallback(query), null, new ReadDbObjectCallback<T>(reader, targetClass),
public <T> List<T> find(String collectionName, DBObject query, DBObject fields, Class<T> targetClass, MongoReader<T> reader) {
return executeEach(new FindCallback(query, fields), null, new ReadDbObjectCallback<T>(reader, targetClass),
collectionName);
}
@ -829,21 +857,34 @@ public class MongoTemplate implements InitializingBean, MongoOperations { @@ -829,21 +857,34 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
/**
* Simple {@link CollectionCallback} that takes a query {@link DBObject} and executes that against the
* {@link DBCollection}.
* Simple {@link CollectionCallback} that takes a query {@link DBObject} plus an optional fields specification
* {@link DBObject} and executes that against the {@link DBCollection}.
*
* @author Oliver Gierke
* @author Thomas Risberg
*/
private static class FindCallback implements CollectionCallback<DBCursor> {
private final DBObject query;
private final DBObject fields;
public FindCallback(DBObject query) {
this(query, null);
}
public FindCallback(DBObject query, DBObject fields) {
this.query = query;
this.fields = fields;
}
public DBCursor doInCollection(DBCollection collection) throws MongoException, DataAccessException {
return collection.find(query);
if (fields == null) {
return collection.find(query);
}
else {
return collection.find(query, fields);
}
}
}

4
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java

@ -95,7 +95,7 @@ public class MongoQuery implements RepositoryQuery { @@ -95,7 +95,7 @@ public class MongoQuery implements RepositoryQuery {
protected List<?> readCollection(DBObject query) {
return template.query(template.getDefaultCollectionName(),
return template.find(template.getDefaultCollectionName(),
query, method.getDomainClass());
}
}
@ -159,7 +159,7 @@ public class MongoQuery implements RepositoryQuery { @@ -159,7 +159,7 @@ public class MongoQuery implements RepositoryQuery {
int count = getCollectionCursor(creator.createQuery()).count();
List<?> result =
template.query(query, method.getDomainClass(),
template.find(query, method.getDomainClass(),
withPagination(pageable));
return new PageImpl(result, pageable, count);

6
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java

@ -108,7 +108,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends @@ -108,7 +108,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
MongoConverter converter = template.getConverter();
List<T> result = template.query(QueryBuilder.start("_id").is(converter.convertObjectId(id)).get(),
List<T> result = template.find(QueryBuilder.start("_id").is(converter.convertObjectId(id)).get(),
getDomainClass());
return result.isEmpty() ? null : result.get(0);
}
@ -202,7 +202,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends @@ -202,7 +202,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
Long count = count();
List<T> list =
template.query(new BasicDBObject(), getDomainClass(),
template.find(new BasicDBObject(), getDomainClass(),
withPagination(pageable));
return new PageImpl<T>(list, pageable, count);
@ -218,7 +218,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends @@ -218,7 +218,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/
public List<T> findAll(final Sort sort) {
return template.query(new BasicDBObject(), getDomainClass(),
return template.find(new BasicDBObject(), getDomainClass(),
withSorting(sort));
}

2
spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java

@ -54,7 +54,7 @@ public class MongoTemplateTests { @@ -54,7 +54,7 @@ public class MongoTemplateTests {
MongoConverter converter = template.getConverter();
List<Person> result = template.query(QueryBuilder.start("_id").is(converter.convertObjectId(person.getId())).get(), Person.class);
List<Person> result = template.find(QueryBuilder.start("_id").is(converter.convertObjectId(person.getId())).get(), Person.class);
assertThat(result.size(), is(1));
assertThat(result, hasItem(person));
}

86
spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateWriteConcernTests.java

@ -1,86 +0,0 @@ @@ -1,86 +0,0 @@
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.document.mongodb;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mongodb.WriteConcern;
/**
* Integration test of the WriteConcern features for {@link MongoTemplate}.
*
* @author Thomas Risberg
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:infrastructure.xml")
public class MongoTemplateWriteConcernTests {
@Autowired
MongoTemplate template;
@Before
public void setUp() {
template.dropCollection(template.getDefaultCollectionName());
}
@After
public void tearDown() {
template.setCollectionWriteConcern(template.getDefaultCollectionName(), WriteConcern.NORMAL);
template.setDatabaseWriteConcern(WriteConcern.NORMAL);
}
@Test
public void testRetrievingTheDatabaseWriteConcern() throws Exception {
WriteConcern wc = template.getDatabaseWriteConcern();
assertNotNull(wc);
}
@Test
public void testRetrievingTheCollectionWriteConcern() throws Exception {
WriteConcern wc = template.getCollectionWriteConcern(template.getDefaultCollectionName());
assertNotNull(wc);
}
@Test
public void testSettingTheDatabaseWriteConcern() throws Exception {
WriteConcern wc = template.getDatabaseWriteConcern();
WriteConcern safe = WriteConcern.SAFE;
template.setDatabaseWriteConcern(safe);
assertEquals(safe.getW(), template.getDatabaseWriteConcern().getW());
assertNotSame(wc.getW(), template.getDatabaseWriteConcern().getW());
}
@Test
public void testSettingTheCollectionWriteConcern() throws Exception {
String coll = template.getDefaultCollectionName();
WriteConcern replicasSafe = WriteConcern.REPLICAS_SAFE;
WriteConcern fsyncSafe = WriteConcern.FSYNC_SAFE;
template.setDatabaseWriteConcern(fsyncSafe);
template.setCollectionWriteConcern(coll, replicasSafe);
assertEquals(replicasSafe.getW(), template.getCollectionWriteConcern(coll).getW());
assertEquals(replicasSafe.fsync(), template.getCollectionWriteConcern(coll).fsync());
}
}

3
spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java

@ -12,6 +12,7 @@ import org.springframework.data.document.analytics.MvcEvent; @@ -12,6 +12,7 @@ import org.springframework.data.document.analytics.MvcEvent;
import org.springframework.data.document.analytics.Parameters;
import org.springframework.data.document.mongodb.MongoReader;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.builder.BasicQuery;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
@ -87,7 +88,7 @@ public class MvcAnalyticsTests { @@ -87,7 +88,7 @@ public class MvcAnalyticsTests {
for (DBObject dbo : mongoTemplate.getCollection("counters").find(query)) {
System.out.println(dbo);
}
List<ControllerCounter> counters = mongoTemplate.queryUsingJavaScript("counters", "{ 'name' : 'SignUpController'} ", ControllerCounter.class);
List<ControllerCounter> counters = mongoTemplate.query("counters", new BasicQuery("{ 'name' : 'SignUpController'} "), ControllerCounter.class);
for (ControllerCounter controllerCounter : counters) {
System.out.println(controllerCounter);
}

Loading…
Cancel
Save