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;
This commit is contained in:
@@ -18,6 +18,8 @@ package org.springframework.data.document.mongodb;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.data.document.mongodb.builder.Query;
|
||||||
|
|
||||||
import com.mongodb.DBCollection;
|
import com.mongodb.DBCollection;
|
||||||
import com.mongodb.DBObject;
|
import com.mongodb.DBObject;
|
||||||
import com.mongodb.WriteConcern;
|
import com.mongodb.WriteConcern;
|
||||||
@@ -44,32 +46,6 @@ public interface MongoOperations {
|
|||||||
* @return The default collection used by this template
|
* @return The default collection used by this template
|
||||||
*/
|
*/
|
||||||
DBCollection getDefaultCollection();
|
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
|
* Execute the a MongoDB command expressed as a JSON string. This will call the method
|
||||||
@@ -406,7 +382,6 @@ public interface MongoOperations {
|
|||||||
* Query for a list of objects of type T from the specified collection, mapping the DBObject using
|
* Query for a list of objects of type T from the specified collection, mapping the DBObject using
|
||||||
* the provided MongoReader.
|
* the provided MongoReader.
|
||||||
*
|
*
|
||||||
*
|
|
||||||
* @param collectionName name of the collection to retrieve the objects from
|
* @param collectionName name of the collection to retrieve the objects from
|
||||||
* @param targetClass the parameterized type of the returned list.
|
* @param targetClass the parameterized type of the returned list.
|
||||||
* @param reader the MongoReader to convert from DBObject to an object.
|
* @param reader the MongoReader to convert from DBObject to an object.
|
||||||
@@ -415,15 +390,75 @@ public interface MongoOperations {
|
|||||||
<T> List<T> getCollection(String collectionName, Class<T> targetClass,
|
<T> List<T> getCollection(String collectionName, Class<T> targetClass,
|
||||||
MongoReader<T> reader);
|
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);
|
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);
|
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);
|
Class<T> targetClass, MongoReader<T> reader);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -433,8 +468,7 @@ public interface MongoOperations {
|
|||||||
* {@see MongoConverter}. Unless configured otherwise, an
|
* {@see MongoConverter}. Unless configured otherwise, an
|
||||||
* instance of SimpleMongoConverter will be used.
|
* instance of SimpleMongoConverter will be used.
|
||||||
*
|
*
|
||||||
* The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to
|
* The query document is specified as a standard DBObject.
|
||||||
* more easily create a query document.
|
|
||||||
*
|
*
|
||||||
* NOTE: A generic criteria API will be introduced in a future release. You can see the
|
* 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
|
* <a href="https://github.com/grails/inconsequential">inconsequential</a> project for an example of how that
|
||||||
@@ -444,7 +478,7 @@ public interface MongoOperations {
|
|||||||
* @param targetClass the parameterized type of the returned list.
|
* @param targetClass the parameterized type of the returned list.
|
||||||
* @return the List of converted objects
|
* @return the List of converted objects
|
||||||
*/
|
*/
|
||||||
<T> List<T> query(DBObject query, Class<T> targetClass);
|
<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.
|
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
|
||||||
@@ -453,8 +487,7 @@ public interface MongoOperations {
|
|||||||
* {@see MongoConverter}. Unless configured otherwise, an
|
* {@see MongoConverter}. Unless configured otherwise, an
|
||||||
* instance of SimpleMongoConverter will be used.
|
* instance of SimpleMongoConverter will be used.
|
||||||
*
|
*
|
||||||
* The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to
|
* The query document is specified as a standard DBObject.
|
||||||
* more easily create a query document.
|
|
||||||
*
|
*
|
||||||
* @param query the query document that specifies the criteria used to find a record
|
* @param query the query document that specifies the criteria used to find a record
|
||||||
* @param targetClass the parameterized type of the returned list.
|
* @param targetClass the parameterized type of the returned list.
|
||||||
@@ -462,21 +495,20 @@ public interface MongoOperations {
|
|||||||
* (apply limits, skips and so on).
|
* (apply limits, skips and so on).
|
||||||
* @return the List of converted objects.
|
* @return the List of converted objects.
|
||||||
*/
|
*/
|
||||||
<T> List<T> query(DBObject query, Class<T> targetClass,
|
<T> List<T> find(DBObject query, Class<T> targetClass,
|
||||||
CursorPreparer preparer);
|
CursorPreparer preparer);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader
|
* 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
|
* The query document is specified as a standard DBObject.
|
||||||
* more easily create a query document.
|
|
||||||
*
|
*
|
||||||
* @param query the query document that specifies the criteria used to find a record
|
* @param query the query document that specifies the criteria used to find a record
|
||||||
* @param targetClass the parameterized type of the returned list.
|
* @param targetClass the parameterized type of the returned list.
|
||||||
* @param reader the MongoReader to convert from DBObject to an object.
|
* @param reader the MongoReader to convert from DBObject to an object.
|
||||||
* @return the List of converted objects.
|
* @return the List of converted objects.
|
||||||
*/
|
*/
|
||||||
<T> List<T> query(DBObject query, Class<T> targetClass,
|
<T> List<T> find(DBObject query, Class<T> targetClass,
|
||||||
MongoReader<T> reader);
|
MongoReader<T> reader);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -486,8 +518,7 @@ public interface MongoOperations {
|
|||||||
* {@see MongoConverter}. Unless configured otherwise, an
|
* {@see MongoConverter}. Unless configured otherwise, an
|
||||||
* instance of SimpleMongoConverter will be used.
|
* instance of SimpleMongoConverter will be used.
|
||||||
*
|
*
|
||||||
* The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to
|
* The query document is specified as a standard DBObject.
|
||||||
* more easily create a query document.
|
|
||||||
*
|
*
|
||||||
* NOTE: A generic criteria API will be introduced in a future release. You can see the
|
* 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
|
* <a href="https://github.com/grails/inconsequential">inconsequential</a> project for an example of how that
|
||||||
@@ -498,7 +529,7 @@ public interface MongoOperations {
|
|||||||
* @param targetClass the parameterized type of the returned list.
|
* @param targetClass the parameterized type of the returned list.
|
||||||
* @return the List of converted objects
|
* @return the List of converted objects
|
||||||
*/
|
*/
|
||||||
<T> List<T> query(String collectionName, DBObject query, Class<T> targetClass);
|
<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.
|
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
|
||||||
@@ -507,8 +538,7 @@ public interface MongoOperations {
|
|||||||
* {@see MongoConverter}. Unless configured otherwise, an
|
* {@see MongoConverter}. Unless configured otherwise, an
|
||||||
* instance of SimpleMongoConverter will be used.
|
* instance of SimpleMongoConverter will be used.
|
||||||
*
|
*
|
||||||
* The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to
|
* The query document is specified as a standard DBObject.
|
||||||
* more easily create a query document.
|
|
||||||
*
|
*
|
||||||
* @param collectionName name of the collection to retrieve the objects from
|
* @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 query the query document that specifies the criteria used to find a record
|
||||||
@@ -517,13 +547,12 @@ public interface MongoOperations {
|
|||||||
* (apply limits, skips and so on).
|
* (apply limits, skips and so on).
|
||||||
* @return the List of converted objects.
|
* @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 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
|
* 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
|
* The query document is specified as a standard DBObject.
|
||||||
* more easily create a query document.
|
|
||||||
*
|
*
|
||||||
* @param collectionName name of the collection to retrieve the objects from
|
* @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 query the query document that specifies the criteria used to find a record
|
||||||
@@ -531,5 +560,111 @@ public interface MongoOperations {
|
|||||||
* @param reader the MongoReader to convert from DBObject to an object.
|
* @param reader the MongoReader to convert from DBObject to an object.
|
||||||
* @return the List of converted objects.
|
* @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 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
|
||||||
|
* 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, 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.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @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> 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 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> find(DBObject query, DBObject fields, 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
|
||||||
|
* 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 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> 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.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @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> 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 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> find(String collectionName, DBObject fields, DBObject query, Class<T> targetClass, MongoReader<T> reader);
|
||||||
}
|
}
|
||||||
@@ -28,6 +28,7 @@ import org.springframework.beans.factory.InitializingBean;
|
|||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||||
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
|
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.jca.cci.core.ConnectionCallback;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
@@ -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)
|
/* (non-Javadoc)
|
||||||
* @see org.springframework.data.document.mongodb.MongoOperations#executeCommand(java.lang.String)
|
* @see org.springframework.data.document.mongodb.MongoOperations#executeCommand(java.lang.String)
|
||||||
*/
|
*/
|
||||||
@@ -683,75 +667,119 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.Class)
|
* @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.Class)
|
||||||
*/
|
*/
|
||||||
public <T> List<T> queryUsingJavaScript(String query, Class<T> targetClass) {
|
public <T> List<T> query(Query query, Class<T> targetClass) {
|
||||||
return query(getDefaultCollectionName(), (DBObject)JSON.parse(query), targetClass); //
|
return query(getDefaultCollectionName(), query, targetClass); //
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
|
* @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) {
|
public <T> List<T> query(Query query, Class<T> targetClass, MongoReader<T> reader) {
|
||||||
return query(getDefaultCollectionName(), (DBObject)JSON.parse(query), targetClass, reader);
|
return query(getDefaultCollectionName(), query, targetClass, reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.String, java.lang.Class)
|
* @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) {
|
public <T> List<T> query(String collectionName, Query query, Class<T> targetClass) {
|
||||||
return query(collectionName, (DBObject)JSON.parse(query), targetClass); //
|
return find(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass); //
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (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)
|
* @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) {
|
public <T> List<T> query(String collectionName, Query query, Class<T> targetClass, MongoReader<T> reader) {
|
||||||
return query(collectionName, (DBObject)JSON.parse(query), targetClass, 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)
|
/* (non-Javadoc)
|
||||||
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class)
|
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class)
|
||||||
*/
|
*/
|
||||||
public <T> List<T> query(DBObject query, Class<T> targetClass) {
|
public <T> List<T> find(DBObject query, Class<T> targetClass) {
|
||||||
return query(getDefaultCollectionName(), query, targetClass); //
|
return find(query, null, targetClass); //
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer)
|
* @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) {
|
public <T> List<T> find(DBObject query, Class<T> targetClass, CursorPreparer preparer) {
|
||||||
return query(getDefaultCollectionName(), query, targetClass, preparer); //
|
return find(query, null, targetClass, preparer); //
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
|
* @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) {
|
public <T> List<T> find(DBObject query, Class<T> targetClass, MongoReader<T> reader) {
|
||||||
return query(getDefaultCollectionName(), query, targetClass, reader);
|
return find(query, null, targetClass, reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class)
|
* @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) {
|
public <T> List<T> find(String collectionName, DBObject query, Class<T> targetClass) {
|
||||||
return query(collectionName, query, targetClass, (CursorPreparer) null);
|
return find(collectionName, query, null, targetClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (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)
|
* @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) {
|
public <T> List<T> find(String collectionName, DBObject query, Class<T> targetClass, CursorPreparer preparer) {
|
||||||
return executeEach(new FindCallback(query), preparer, new ReadDbObjectCallback<T>(mongoConverter, targetClass),
|
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);
|
collectionName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (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)
|
* @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) {
|
public <T> List<T> find(String collectionName, DBObject query, DBObject fields, Class<T> targetClass, MongoReader<T> reader) {
|
||||||
return executeEach(new FindCallback(query), null, new ReadDbObjectCallback<T>(reader, targetClass),
|
return executeEach(new FindCallback(query, fields), null, new ReadDbObjectCallback<T>(reader, targetClass),
|
||||||
collectionName);
|
collectionName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -829,21 +857,34 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple {@link CollectionCallback} that takes a query {@link DBObject} and executes that against the
|
* Simple {@link CollectionCallback} that takes a query {@link DBObject} plus an optional fields specification
|
||||||
* {@link DBCollection}.
|
* {@link DBObject} and executes that against the {@link DBCollection}.
|
||||||
*
|
*
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
|
* @author Thomas Risberg
|
||||||
*/
|
*/
|
||||||
private static class FindCallback implements CollectionCallback<DBCursor> {
|
private static class FindCallback implements CollectionCallback<DBCursor> {
|
||||||
|
|
||||||
private final DBObject query;
|
private final DBObject query;
|
||||||
|
|
||||||
|
private final DBObject fields;
|
||||||
|
|
||||||
public FindCallback(DBObject query) {
|
public FindCallback(DBObject query) {
|
||||||
|
this(query, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FindCallback(DBObject query, DBObject fields) {
|
||||||
this.query = query;
|
this.query = query;
|
||||||
|
this.fields = fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DBCursor doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ public class MongoQuery implements RepositoryQuery {
|
|||||||
|
|
||||||
protected List<?> readCollection(DBObject query) {
|
protected List<?> readCollection(DBObject query) {
|
||||||
|
|
||||||
return template.query(template.getDefaultCollectionName(),
|
return template.find(template.getDefaultCollectionName(),
|
||||||
query, method.getDomainClass());
|
query, method.getDomainClass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -159,7 +159,7 @@ public class MongoQuery implements RepositoryQuery {
|
|||||||
|
|
||||||
int count = getCollectionCursor(creator.createQuery()).count();
|
int count = getCollectionCursor(creator.createQuery()).count();
|
||||||
List<?> result =
|
List<?> result =
|
||||||
template.query(query, method.getDomainClass(),
|
template.find(query, method.getDomainClass(),
|
||||||
withPagination(pageable));
|
withPagination(pageable));
|
||||||
|
|
||||||
return new PageImpl(result, pageable, count);
|
return new PageImpl(result, pageable, count);
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
|
|||||||
|
|
||||||
MongoConverter converter = template.getConverter();
|
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());
|
getDomainClass());
|
||||||
return result.isEmpty() ? null : result.get(0);
|
return result.isEmpty() ? null : result.get(0);
|
||||||
}
|
}
|
||||||
@@ -202,7 +202,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
|
|||||||
Long count = count();
|
Long count = count();
|
||||||
|
|
||||||
List<T> list =
|
List<T> list =
|
||||||
template.query(new BasicDBObject(), getDomainClass(),
|
template.find(new BasicDBObject(), getDomainClass(),
|
||||||
withPagination(pageable));
|
withPagination(pageable));
|
||||||
|
|
||||||
return new PageImpl<T>(list, pageable, count);
|
return new PageImpl<T>(list, pageable, count);
|
||||||
@@ -218,7 +218,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
|
|||||||
*/
|
*/
|
||||||
public List<T> findAll(final Sort sort) {
|
public List<T> findAll(final Sort sort) {
|
||||||
|
|
||||||
return template.query(new BasicDBObject(), getDomainClass(),
|
return template.find(new BasicDBObject(), getDomainClass(),
|
||||||
withSorting(sort));
|
withSorting(sort));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ public class MongoTemplateTests {
|
|||||||
|
|
||||||
MongoConverter converter = template.getConverter();
|
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.size(), is(1));
|
||||||
assertThat(result, hasItem(person));
|
assertThat(result, hasItem(person));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,6 +12,7 @@ import org.springframework.data.document.analytics.MvcEvent;
|
|||||||
import org.springframework.data.document.analytics.Parameters;
|
import org.springframework.data.document.analytics.Parameters;
|
||||||
import org.springframework.data.document.mongodb.MongoReader;
|
import org.springframework.data.document.mongodb.MongoReader;
|
||||||
import org.springframework.data.document.mongodb.MongoTemplate;
|
import org.springframework.data.document.mongodb.MongoTemplate;
|
||||||
|
import org.springframework.data.document.mongodb.builder.BasicQuery;
|
||||||
|
|
||||||
import com.mongodb.BasicDBList;
|
import com.mongodb.BasicDBList;
|
||||||
import com.mongodb.BasicDBObject;
|
import com.mongodb.BasicDBObject;
|
||||||
@@ -87,7 +88,7 @@ public class MvcAnalyticsTests {
|
|||||||
for (DBObject dbo : mongoTemplate.getCollection("counters").find(query)) {
|
for (DBObject dbo : mongoTemplate.getCollection("counters").find(query)) {
|
||||||
System.out.println(dbo);
|
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) {
|
for (ControllerCounter controllerCounter : counters) {
|
||||||
System.out.println(controllerCounter);
|
System.out.println(controllerCounter);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user