|
|
|
@ -40,7 +40,7 @@ import com.mongodb.MongoException; |
|
|
|
import com.mongodb.WriteResult; |
|
|
|
import com.mongodb.WriteResult; |
|
|
|
import com.mongodb.util.JSON; |
|
|
|
import com.mongodb.util.JSON; |
|
|
|
|
|
|
|
|
|
|
|
public class MongoTemplate implements InitializingBean { |
|
|
|
public class MongoTemplate implements InitializingBean, MongoOperations { |
|
|
|
|
|
|
|
|
|
|
|
private String defaultCollectionName; |
|
|
|
private String defaultCollectionName; |
|
|
|
|
|
|
|
|
|
|
|
@ -104,21 +104,30 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
this.databaseName = databaseName; |
|
|
|
this.databaseName = databaseName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#getDefaultCollectionName() |
|
|
|
|
|
|
|
*/ |
|
|
|
public String getDefaultCollectionName() { |
|
|
|
public String getDefaultCollectionName() { |
|
|
|
return defaultCollectionName; |
|
|
|
return defaultCollectionName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/* (non-Javadoc) |
|
|
|
* @return The default collection used by this template |
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#getDefaultCollection() |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public DBCollection getDefaultCollection() { |
|
|
|
public DBCollection getDefaultCollection() { |
|
|
|
return getDb().getCollection(getDefaultCollectionName()); |
|
|
|
return getDb().getCollection(getDefaultCollectionName()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#executeCommand(java.lang.String) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void executeCommand(String jsonCommand) { |
|
|
|
public void executeCommand(String jsonCommand) { |
|
|
|
executeCommand((DBObject)JSON.parse(jsonCommand)); |
|
|
|
executeCommand((DBObject)JSON.parse(jsonCommand)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#executeCommand(com.mongodb.DBObject) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void executeCommand(DBObject command) { |
|
|
|
public void executeCommand(DBObject command) { |
|
|
|
CommandResult cr = getDb().command(command); |
|
|
|
CommandResult cr = getDb().command(command); |
|
|
|
String err = cr.getErrorMessage(); |
|
|
|
String err = cr.getErrorMessage(); |
|
|
|
@ -128,13 +137,8 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/* (non-Javadoc) |
|
|
|
* Executes a {@link DBCallback} translating any exceptions as necessary |
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.DBCallback) |
|
|
|
* |
|
|
|
|
|
|
|
* @param <T> The return type |
|
|
|
|
|
|
|
* @param action The action to execute |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return The return value of the {@link DBCallback} |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public <T> T execute(DBCallback<T> action) { |
|
|
|
public <T> T execute(DBCallback<T> action) { |
|
|
|
DB db = getDb(); |
|
|
|
DB db = getDb(); |
|
|
|
@ -146,24 +150,15 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/* (non-Javadoc) |
|
|
|
* Executes the given {@link CollectionCallback} on the default collection. |
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.CollectionCallback) |
|
|
|
* |
|
|
|
|
|
|
|
* @param <T> |
|
|
|
|
|
|
|
* @param callback |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public <T> T execute(CollectionCallback<T> callback) { |
|
|
|
public <T> T execute(CollectionCallback<T> callback) { |
|
|
|
return execute(callback, defaultCollectionName); |
|
|
|
return execute(callback, defaultCollectionName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/* (non-Javadoc) |
|
|
|
* Executes the given {@link CollectionCallback} on the collection of the given name. |
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.CollectionCallback, java.lang.String) |
|
|
|
* |
|
|
|
|
|
|
|
* @param <T> |
|
|
|
|
|
|
|
* @param callback |
|
|
|
|
|
|
|
* @param collectionName |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public <T> T execute(CollectionCallback<T> callback, String collectionName) { |
|
|
|
public <T> T execute(CollectionCallback<T> callback, String collectionName) { |
|
|
|
|
|
|
|
|
|
|
|
@ -174,6 +169,9 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#executeInSession(org.springframework.data.document.mongodb.DBCallback) |
|
|
|
|
|
|
|
*/ |
|
|
|
public <T> T executeInSession(DBCallback<T> action) { |
|
|
|
public <T> T executeInSession(DBCallback<T> action) { |
|
|
|
DB db = getDb(); |
|
|
|
DB db = getDb(); |
|
|
|
db.requestStart(); |
|
|
|
db.requestStart(); |
|
|
|
@ -186,6 +184,9 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#createCollection(java.lang.String) |
|
|
|
|
|
|
|
*/ |
|
|
|
public DBCollection createCollection(String collectionName) { |
|
|
|
public DBCollection createCollection(String collectionName) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
return getDb().createCollection(collectionName, null); |
|
|
|
return getDb().createCollection(collectionName, null); |
|
|
|
@ -194,6 +195,9 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#createCollection(java.lang.String, org.springframework.data.document.mongodb.CollectionOptions) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void createCollection(String collectionName, CollectionOptions collectionOptions) { |
|
|
|
public void createCollection(String collectionName, CollectionOptions collectionOptions) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
getDb().createCollection(collectionName, convertToDbObject(collectionOptions)); |
|
|
|
getDb().createCollection(collectionName, convertToDbObject(collectionOptions)); |
|
|
|
@ -202,6 +206,9 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#getCollection(java.lang.String) |
|
|
|
|
|
|
|
*/ |
|
|
|
public DBCollection getCollection(String collectionName) { |
|
|
|
public DBCollection getCollection(String collectionName) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
return getDb().getCollection(collectionName); |
|
|
|
return getDb().getCollection(collectionName); |
|
|
|
@ -211,6 +218,9 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#collectionExists(java.lang.String) |
|
|
|
|
|
|
|
*/ |
|
|
|
public boolean collectionExists(String collectionName) { |
|
|
|
public boolean collectionExists(String collectionName) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
return getDb().collectionExists(collectionName); |
|
|
|
return getDb().collectionExists(collectionName); |
|
|
|
@ -219,6 +229,9 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#dropCollection(java.lang.String) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void dropCollection(String collectionName) { |
|
|
|
public void dropCollection(String collectionName) { |
|
|
|
getDb().getCollection(collectionName) |
|
|
|
getDb().getCollection(collectionName) |
|
|
|
.drop(); |
|
|
|
.drop(); |
|
|
|
@ -235,14 +248,23 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.Object) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void insert(Object objectToSave) { |
|
|
|
public void insert(Object objectToSave) { |
|
|
|
insert(getRequiredDefaultCollectionName(), objectToSave); |
|
|
|
insert(getRequiredDefaultCollectionName(), objectToSave); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.String, java.lang.Object) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void insert(String collectionName, Object objectToSave) { |
|
|
|
public void insert(String collectionName, Object objectToSave) { |
|
|
|
insert(collectionName, objectToSave, this.mongoConverter); |
|
|
|
insert(collectionName, objectToSave, this.mongoConverter); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.String, T, org.springframework.data.document.mongodb.MongoWriter) |
|
|
|
|
|
|
|
*/ |
|
|
|
public <T> void insert(String collectionName, T objectToSave, MongoWriter<T> writer) { |
|
|
|
public <T> void insert(String collectionName, T objectToSave, MongoWriter<T> writer) { |
|
|
|
BasicDBObject dbDoc = new BasicDBObject(); |
|
|
|
BasicDBObject dbDoc = new BasicDBObject(); |
|
|
|
writer.write(objectToSave, dbDoc); |
|
|
|
writer.write(objectToSave, dbDoc); |
|
|
|
@ -250,14 +272,23 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
populateIdIfNecessary(objectToSave, _id); |
|
|
|
populateIdIfNecessary(objectToSave, _id); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.util.List) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void insertList(List<Object> listToSave) { |
|
|
|
public void insertList(List<Object> listToSave) { |
|
|
|
insertList(getRequiredDefaultCollectionName(), listToSave); |
|
|
|
insertList(getRequiredDefaultCollectionName(), listToSave); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.lang.String, java.util.List) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void insertList(String collectionName, List<Object> listToSave) { |
|
|
|
public void insertList(String collectionName, List<Object> listToSave) { |
|
|
|
insertList(collectionName, listToSave, this.mongoConverter); |
|
|
|
insertList(collectionName, listToSave, this.mongoConverter); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.lang.String, java.util.List, org.springframework.data.document.mongodb.MongoWriter) |
|
|
|
|
|
|
|
*/ |
|
|
|
public <T> void insertList(String collectionName, List<T> listToSave, MongoWriter<T> writer) { |
|
|
|
public <T> void insertList(String collectionName, List<T> listToSave, MongoWriter<T> writer) { |
|
|
|
List<DBObject> dbObjectList = new ArrayList<DBObject>(); |
|
|
|
List<DBObject> dbObjectList = new ArrayList<DBObject>(); |
|
|
|
for (T o : listToSave) { |
|
|
|
for (T o : listToSave) { |
|
|
|
@ -272,14 +303,23 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#save(java.lang.Object) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void save(Object objectToSave) { |
|
|
|
public void save(Object objectToSave) { |
|
|
|
save(getRequiredDefaultCollectionName(), objectToSave); |
|
|
|
save(getRequiredDefaultCollectionName(), objectToSave); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#save(java.lang.String, java.lang.Object) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void save(String collectionName, Object objectToSave) { |
|
|
|
public void save(String collectionName, Object objectToSave) { |
|
|
|
save(collectionName, objectToSave, this.mongoConverter); |
|
|
|
save(collectionName, objectToSave, this.mongoConverter); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#save(java.lang.String, T, org.springframework.data.document.mongodb.MongoWriter) |
|
|
|
|
|
|
|
*/ |
|
|
|
public <T> void save(String collectionName, T objectToSave, MongoWriter<T> writer) { |
|
|
|
public <T> void save(String collectionName, T objectToSave, MongoWriter<T> writer) { |
|
|
|
BasicDBObject dbDoc = new BasicDBObject(); |
|
|
|
BasicDBObject dbDoc = new BasicDBObject(); |
|
|
|
writer.write(objectToSave, dbDoc); |
|
|
|
writer.write(objectToSave, dbDoc); |
|
|
|
@ -337,10 +377,16 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#updateFirst(com.mongodb.DBObject, com.mongodb.DBObject) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void updateFirst(DBObject queryDoc, DBObject updateDoc) { |
|
|
|
public void updateFirst(DBObject queryDoc, DBObject updateDoc) { |
|
|
|
updateFirst(getRequiredDefaultCollectionName(), queryDoc, updateDoc); |
|
|
|
updateFirst(getRequiredDefaultCollectionName(), queryDoc, updateDoc); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#updateFirst(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void updateFirst(String collectionName, DBObject queryDoc, DBObject updateDoc) { |
|
|
|
public void updateFirst(String collectionName, DBObject queryDoc, DBObject updateDoc) { |
|
|
|
WriteResult wr = null; |
|
|
|
WriteResult wr = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
@ -350,10 +396,16 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#updateMulti(com.mongodb.DBObject, com.mongodb.DBObject) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void updateMulti(DBObject queryDoc, DBObject updateDoc) { |
|
|
|
public void updateMulti(DBObject queryDoc, DBObject updateDoc) { |
|
|
|
updateMulti(getRequiredDefaultCollectionName(), queryDoc, updateDoc); |
|
|
|
updateMulti(getRequiredDefaultCollectionName(), queryDoc, updateDoc); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#updateMulti(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void updateMulti(String collectionName, DBObject queryDoc, DBObject updateDoc) { |
|
|
|
public void updateMulti(String collectionName, DBObject queryDoc, DBObject updateDoc) { |
|
|
|
WriteResult wr = null; |
|
|
|
WriteResult wr = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
@ -363,10 +415,16 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#remove(com.mongodb.DBObject) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void remove(DBObject queryDoc) { |
|
|
|
public void remove(DBObject queryDoc) { |
|
|
|
remove(getRequiredDefaultCollectionName(), queryDoc); |
|
|
|
remove(getRequiredDefaultCollectionName(), queryDoc); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#remove(java.lang.String, com.mongodb.DBObject) |
|
|
|
|
|
|
|
*/ |
|
|
|
public void remove(String collectionName, DBObject queryDoc) { |
|
|
|
public void remove(String collectionName, DBObject queryDoc) { |
|
|
|
WriteResult wr = null; |
|
|
|
WriteResult wr = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
@ -377,6 +435,9 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#getCollection(java.lang.Class) |
|
|
|
|
|
|
|
*/ |
|
|
|
public <T> List<T> getCollection(Class<T> targetClass) { |
|
|
|
public <T> List<T> getCollection(Class<T> targetClass) { |
|
|
|
|
|
|
|
|
|
|
|
List<T> results = new ArrayList<T>(); |
|
|
|
List<T> results = new ArrayList<T>(); |
|
|
|
@ -391,6 +452,9 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
return results; |
|
|
|
return results; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#getCollection(java.lang.String, java.lang.Class) |
|
|
|
|
|
|
|
*/ |
|
|
|
public <T> List<T> getCollection(String collectionName, Class<T> targetClass) { |
|
|
|
public <T> List<T> getCollection(String collectionName, Class<T> targetClass) { |
|
|
|
|
|
|
|
|
|
|
|
List<T> results = new ArrayList<T>(); |
|
|
|
List<T> results = new ArrayList<T>(); |
|
|
|
@ -405,6 +469,9 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
return results; |
|
|
|
return results; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc) |
|
|
|
|
|
|
|
* @see org.springframework.data.document.mongodb.MongoOperations#getCollection(java.lang.String, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) |
|
|
|
|
|
|
|
*/ |
|
|
|
public <T> List<T> getCollection(String collectionName, Class<T> targetClass, MongoReader<T> reader) { |
|
|
|
public <T> List<T> getCollection(String collectionName, Class<T> targetClass, MongoReader<T> reader) { |
|
|
|
List<T> results = new ArrayList<T>(); |
|
|
|
List<T> results = new ArrayList<T>(); |
|
|
|
DBCollection collection = getDb().getCollection(collectionName); |
|
|
|
DBCollection collection = getDb().getCollection(collectionName); |
|
|
|
@ -416,18 +483,30 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
|
|
|
|
|
|
|
|
// Queries that take JavaScript to express the query.
|
|
|
|
// Queries that take JavaScript to express the query.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (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) { |
|
|
|
public <T> List<T> queryUsingJavaScript(String query, Class<T> targetClass) { |
|
|
|
return query(getDefaultCollectionName(), (DBObject)JSON.parse(query), targetClass); //
|
|
|
|
return query(getDefaultCollectionName(), (DBObject)JSON.parse(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) { |
|
|
|
public <T> List<T> queryUsingJavaScript(String query, Class<T> targetClass, MongoReader<T> reader) { |
|
|
|
return query(getDefaultCollectionName(), (DBObject)JSON.parse(query), targetClass, reader); |
|
|
|
return query(getDefaultCollectionName(), (DBObject)JSON.parse(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) { |
|
|
|
public <T> List<T> queryUsingJavaScript(String collectionName, String query, Class<T> targetClass) { |
|
|
|
return query(collectionName, (DBObject)JSON.parse(query), targetClass); //
|
|
|
|
return query(collectionName, (DBObject)JSON.parse(query), 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) { |
|
|
|
public <T> List<T> queryUsingJavaScript(String collectionName, String query, Class<T> targetClass, MongoReader<T> reader) { |
|
|
|
return query(collectionName, (DBObject)JSON.parse(query), targetClass, reader); |
|
|
|
return query(collectionName, (DBObject)JSON.parse(query), targetClass, reader); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -435,22 +514,37 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
|
|
|
|
|
|
|
|
// Queries that take DBObject to express the query
|
|
|
|
// Queries 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) { |
|
|
|
public <T> List<T> query(DBObject query, Class<T> targetClass) { |
|
|
|
return query(getDefaultCollectionName(), query, targetClass); //
|
|
|
|
return query(getDefaultCollectionName(), query, 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) { |
|
|
|
public <T> List<T> query(DBObject query, Class<T> targetClass, CursorPreparer preparer) { |
|
|
|
return query(getDefaultCollectionName(), query, targetClass, preparer); //
|
|
|
|
return query(getDefaultCollectionName(), query, 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) { |
|
|
|
public <T> List<T> query(DBObject query, Class<T> targetClass, MongoReader<T> reader) { |
|
|
|
return query(getDefaultCollectionName(), query, targetClass, reader); |
|
|
|
return query(getDefaultCollectionName(), query, 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) { |
|
|
|
public <T> List<T> query(String collectionName, DBObject query, Class<T> targetClass) { |
|
|
|
return query(collectionName, query, targetClass, (CursorPreparer) null); |
|
|
|
return query(collectionName, query, 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> query(String collectionName, DBObject query, Class<T> targetClass, CursorPreparer preparer) { |
|
|
|
public <T> List<T> query(String collectionName, DBObject query, Class<T> targetClass, CursorPreparer preparer) { |
|
|
|
DBCollection collection = getDb().getCollection(collectionName); |
|
|
|
DBCollection collection = getDb().getCollection(collectionName); |
|
|
|
List<T> results = new ArrayList<T>(); |
|
|
|
List<T> results = new ArrayList<T>(); |
|
|
|
@ -468,6 +562,9 @@ public class MongoTemplate implements InitializingBean { |
|
|
|
return results; |
|
|
|
return results; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (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) { |
|
|
|
public <T> List<T> query(String collectionName, DBObject query, Class<T> targetClass, MongoReader<T> reader) { |
|
|
|
DBCollection collection = getDb().getCollection(collectionName); |
|
|
|
DBCollection collection = getDb().getCollection(collectionName); |
|
|
|
List<T> results = new ArrayList<T>(); |
|
|
|
List<T> results = new ArrayList<T>(); |
|
|
|
|