diff --git a/src/main/java/org/springframework/datastore/document/mongodb/MongoTemplate.java b/src/main/java/org/springframework/datastore/document/mongodb/MongoTemplate.java index ab688458e..d91b01cb6 100644 --- a/src/main/java/org/springframework/datastore/document/mongodb/MongoTemplate.java +++ b/src/main/java/org/springframework/datastore/document/mongodb/MongoTemplate.java @@ -35,6 +35,7 @@ import com.mongodb.DBCollection; import com.mongodb.DBObject; import com.mongodb.MongoException; import com.mongodb.WriteResult; +import com.mongodb.util.JSON; public class MongoTemplate extends AbstractDocumentStoreTemplate { @@ -118,15 +119,32 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate { if (operands.length != values.length) { throw new InvalidDocumentStoreApiUageException("The number of operands and values must match"); } - List results = new ArrayList(); DBObject query = new BasicDBObject(); DBObject condition = new BasicDBObject(); for (int i = 0; i < operands.length; i++) { condition.put(operands[i], values[i]); } query.put(fieldName, condition); - System.out.println("--> " + query); + return queryForList(collectionName, query, mapper); + } + + public List queryForList(String collectionName, String query, Class targetClass) { + DocumentMapper mapper = MongoBeanPropertyDocumentMapper.newInstance(targetClass); + return queryForList(collectionName, query, mapper); + } + + public List queryForList(String collectionName, String query, DocumentMapper mapper) { + return queryForList(collectionName, (DBObject)JSON.parse(query), mapper); + } + + public List queryForList(String collectionName, DBObject query, Class targetClass) { + DocumentMapper mapper = MongoBeanPropertyDocumentMapper.newInstance(targetClass); + return queryForList(collectionName, query, mapper); + } + + public List queryForList(String collectionName, DBObject query, DocumentMapper mapper) { DBCollection collection = getConnection().getCollection(collectionName); + List results = new ArrayList(); for (DBObject dbo : collection.find(query)) { results.add(mapper.mapDocument(dbo)); }