Skip to content

Commit 8c4219b

Browse files
author
Mark Pollack
committed
Add additional remove methods to MongoTemplate and make consistent with write handling of String <-> ObjectId conversion when using SimpleMongoConverter
Update docs
1 parent e7d47c4 commit 8c4219b

File tree

7 files changed

+254
-43
lines changed

7 files changed

+254
-43
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.mongodb.DBObject;
2424
import com.mongodb.WriteResult;
2525

26+
import org.bson.types.ObjectId;
2627
import org.springframework.data.document.mongodb.index.IndexDefinition;
2728
import org.springframework.data.document.mongodb.query.Query;
2829
import org.springframework.data.document.mongodb.query.Update;
@@ -690,13 +691,30 @@ WriteResult updateFirst(String collectionName, Query query,
690691
WriteResult updateMulti(String collectionName, Query query,
691692
Update update);
692693

694+
/**
695+
* Remove the given object from the collection by Id
696+
* @param object
697+
*/
698+
void remove(Object object);
699+
700+
701+
693702
/**
694703
* Remove all documents from the default collection that match the provided query document criteria.
695704
*
696705
* @param queryDoc the query document that specifies the criteria used to remove a record
697706
*/
698707
void remove(Query query);
699-
708+
709+
/**
710+
* Remove all documents from the default collection that matchthe provided query document critera. The
711+
* Class parameter is use to help convert the Id of the object if it is present in the query.
712+
* @param <T>
713+
* @param query
714+
* @param targetClass
715+
*/
716+
<T> void remove(Query query, Class<T> targetClass);
717+
700718
/**
701719
* Remove all documents from the specified collection that match the provided query document criteria.
702720
*

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

Lines changed: 83 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.data.document.mongodb;
1818

19+
import static org.springframework.data.document.mongodb.query.Criteria.whereId;
20+
import static org.springframework.data.document.mongodb.query.Query.query;
21+
1922
import java.beans.IntrospectionException;
2023
import java.beans.PropertyDescriptor;
2124
import java.lang.reflect.InvocationTargetException;
@@ -165,7 +168,14 @@ public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionN
165168
if (writeResultChecking != null) {
166169
this.writeResultChecking = writeResultChecking;
167170
}
168-
setMongoConverter(mongoConverter == null ? new SimpleMongoConverter() : mongoConverter);
171+
if (mongoConverter == null) {
172+
SimpleMongoConverter smc = new SimpleMongoConverter();
173+
smc.afterPropertiesSet();
174+
setMongoConverter(smc);
175+
} else {
176+
setMongoConverter(mongoConverter);
177+
}
178+
//setMongoConverter(mongoConverter == null ? new SimpleMongoConverter() : mongoConverter);
169179
}
170180

171181
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
@@ -808,33 +818,50 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
808818
* @see org.springframework.data.document.mongodb.MongoOperations#remove(com.mongodb.DBObject)
809819
*/
810820
public void remove(Query query) {
811-
remove(getRequiredDefaultCollectionName(), query);
812-
}
813-
821+
remove(query, null);
822+
}
823+
824+
public void remove(Object object) {
825+
Object idValue = this.getIdValue(object);
826+
remove(new Query(whereId().is(idValue)), object.getClass());
827+
}
828+
829+
public <T> void remove(Query query, Class<T> targetClass) {
830+
remove(getEntityCollection(targetClass), query, targetClass);
831+
}
832+
833+
public <T> void remove(String collectionName, final Query query, Class<T> targetClass) {
834+
if (query == null) {
835+
throw new InvalidDataAccessApiUsageException("Query passed in to remove can't be null");
836+
}
837+
final DBObject queryObject = query.getQueryObject();
838+
if (targetClass == null) {
839+
substituteMappedIdIfNecessary(queryObject);
840+
} else {
841+
substituteMappedIdIfNecessary(queryObject, targetClass, this.mongoConverter);
842+
}
843+
if (LOGGER.isDebugEnabled()) {
844+
LOGGER.debug("remove using query: " + queryObject);
845+
}
846+
execute(collectionName, new CollectionCallback<Void>() {
847+
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
848+
WriteResult wr = null;
849+
if (writeConcern == null) {
850+
wr = collection.remove(queryObject);
851+
} else {
852+
wr = collection.remove(queryObject, writeConcern);
853+
}
854+
handleAnyWriteResultErrors(wr, queryObject, "remove");
855+
return null;
856+
}
857+
});
858+
}
859+
814860
/* (non-Javadoc)
815861
* @see org.springframework.data.document.mongodb.MongoOperations#remove(java.lang.String, com.mongodb.DBObject)
816862
*/
817863
public void remove(String collectionName, final Query query) {
818-
if (query == null) {
819-
throw new InvalidDataAccessApiUsageException("Query passed in to remove can't be null");
820-
}
821-
final DBObject queryObject = query.getQueryObject();
822-
substituteMappedIdIfNecessary(queryObject);
823-
if (LOGGER.isDebugEnabled()) {
824-
LOGGER.debug("remove using query: " + queryObject);
825-
}
826-
execute(collectionName, new CollectionCallback<Void>() {
827-
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
828-
WriteResult wr = null;
829-
if (writeConcern == null) {
830-
wr = collection.remove(queryObject);
831-
} else {
832-
wr = collection.remove(queryObject, writeConcern);
833-
}
834-
handleAnyWriteResultErrors(wr, queryObject, "remove");
835-
return null;
836-
}
837-
});
864+
remove(collectionName, query, null);
838865
}
839866

840867

@@ -909,9 +936,6 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
909936
readerToUse = this.mongoConverter;
910937
}
911938
substituteMappedIdIfNecessary(query, targetClass, readerToUse);
912-
if (LOGGER.isDebugEnabled()) {
913-
LOGGER.debug("findOne using query: " + query + " fields: " + fields + " for class: " + targetClass);
914-
}
915939
return execute(new FindOneCallback(query, fields), new ReadDbObjectCallback<T>(readerToUse, targetClass),
916940
collectionName);
917941
}
@@ -1006,6 +1030,32 @@ protected <T> T doFindAndRemove(String collectionName, DBObject query, DBObject
10061030
collectionName);
10071031
}
10081032

1033+
protected Object getIdValue(Object object) {
1034+
if (null != mappingContext) {
1035+
PersistentEntity<?> entity = mappingContext.getPersistentEntity(object.getClass());
1036+
if (null != entity) {
1037+
PersistentProperty idProp = entity.getIdProperty();
1038+
if (null != idProp) {
1039+
try {
1040+
return MappingBeanHelper.getProperty(object, idProp, Object.class, true);
1041+
} catch (IllegalAccessException e) {
1042+
throw new MappingException(e.getMessage(), e);
1043+
} catch (InvocationTargetException e) {
1044+
throw new MappingException(e.getMessage(), e);
1045+
}
1046+
}
1047+
}
1048+
}
1049+
1050+
ConfigurablePropertyAccessor bw = PropertyAccessorFactory.forDirectFieldAccess(object);
1051+
MongoPropertyDescriptor idDescriptor = new MongoPropertyDescriptors(object.getClass()).getIdDescriptor();
1052+
1053+
if (idDescriptor == null) {
1054+
return null;
1055+
}
1056+
return bw.getPropertyValue(idDescriptor.getName());
1057+
1058+
}
10091059
/**
10101060
* Populates the id property of the saved object, if it's not set already.
10111061
*
@@ -1249,8 +1299,14 @@ public FindOneCallback(DBObject query, DBObject fields) {
12491299

12501300
public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException {
12511301
if (fields == null) {
1302+
if (LOGGER.isDebugEnabled()) {
1303+
LOGGER.debug("findOne using query: " + query + " in db.collection: " + collection.getFullName());
1304+
}
12521305
return collection.findOne(query);
12531306
} else {
1307+
if (LOGGER.isDebugEnabled()) {
1308+
LOGGER.debug("findOne using query: " + query + " fields: " + fields + " in db.collection: " + collection.getFullName());
1309+
}
12541310
return collection.findOne(query, fields);
12551311
}
12561312
}

spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Criteria.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ protected Criteria(List<Criteria> criteriaChain, String key) {
5959
public static Criteria where(String key) {
6060
return new Criteria(key);
6161
}
62+
63+
public static Criteria whereId() {
64+
return new Criteria("id");
65+
}
6266

6367
/**
6468
* Static factory method to create a Criteria using the provided key

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

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,79 @@
1+
/*
2+
* Copyright 2002-2010 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.document.mongodb;
217

18+
import java.util.List;
19+
20+
import org.apache.commons.logging.Log;
21+
import org.apache.commons.logging.LogFactory;
322
import org.springframework.beans.factory.annotation.Autowired;
4-
import static org.springframework.data.document.mongodb.query.Criteria.where;
23+
import org.springframework.context.ApplicationContext;
24+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
25+
26+
import static org.springframework.data.document.mongodb.query.Criteria.*;
527
import org.springframework.data.document.mongodb.query.Query;
628
import static org.springframework.data.document.mongodb.query.Query.query;
729
import org.springframework.data.document.mongodb.query.Update;
830
import static org.springframework.data.document.mongodb.query.Update.update;
931

1032
public class PersonExample {
1133

34+
private static final Log log = LogFactory.getLog(PersonExample.class);
1235
@Autowired
1336
private MongoOperations mongoOps;
1437

38+
public static void main(String[] args) {
39+
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonExampleAppConfig.class);
40+
PersonExample example = applicationContext.getBean(PersonExample.class);
41+
example.doWork();
42+
}
43+
1544
public void doWork() {
16-
Person p = new Person();
45+
mongoOps.dropCollection("personexample");
46+
47+
PersonWithIdPropertyOfTypeString p = new PersonWithIdPropertyOfTypeString();
1748
p.setFirstName("Sven");
1849
p.setAge(22);
19-
50+
2051
mongoOps.save(p);
52+
log.debug("Saved: " + p);
53+
54+
p = mongoOps.findOne(query(whereId().is(p.getId())), PersonWithIdPropertyOfTypeString.class);
55+
56+
log.debug("Found: " + p);
57+
58+
// mongoOps.updateFirst(new Query(where("firstName").is("Sven")), new Update().set("age", 24));
59+
60+
// mongoOps.updateFirst(new Query(where("firstName").is("Sven")), update("age", 24));
61+
62+
mongoOps.updateFirst(query(where("firstName").is("Sven")), update("age", 24));
63+
p = mongoOps.findOne(query(whereId().is(p.getId())), PersonWithIdPropertyOfTypeString.class);
64+
log.debug("Updated: " + p);
65+
66+
67+
//mongoOps.remove( query(whereId().is(p.getId())), p.getClass());
68+
69+
mongoOps.remove(p);
2170

22-
System.out.println(p.getId());
71+
List<PersonWithIdPropertyOfTypeString> people = mongoOps.getCollection(PersonWithIdPropertyOfTypeString.class);
2372

24-
mongoOps.updateFirst(new Query(where("firstName").is("Sven")), new Update().set("age", 24));
73+
//PersonWithIdPropertyOfTypeString p2 = mongoOps.findOne(query(whereId().is(p.getId())), PersonWithIdPropertyOfTypeString.class);
2574

26-
mongoOps.updateFirst(new Query(where("firstName").is("Sven")), update("age", 24));
75+
log.debug("Number of people = : " + people.size());
2776

28-
mongoOps.updateFirst(query(where("firstName").is("Sven")), update("age", 24));
2977
}
3078

3179

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-2010 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.document.mongodb;
17+
18+
import org.springframework.context.annotation.Bean;
19+
import org.springframework.context.annotation.Configuration;
20+
21+
import com.mongodb.Mongo;
22+
23+
@Configuration
24+
public class PersonExampleAppConfig {
25+
26+
@Bean
27+
public Mongo mongo() throws Exception {
28+
return new Mongo("localhost");
29+
}
30+
31+
@Bean
32+
public MongoTemplate mongoTemplate() throws Exception {
33+
return new MongoTemplate(mongo(), "database", "personexample");
34+
}
35+
36+
@Bean
37+
public PersonExample personExample()
38+
{
39+
return new PersonExample();
40+
}
41+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,11 @@ public int getAge() {
4747
public void setAge(int age) {
4848
this.age = age;
4949
}
50+
51+
@Override
52+
public String toString() {
53+
return "PersonWithIdPropertyOfTypeString [id=" + id + ", firstName="
54+
+ firstName + ", age=" + age + "]";
55+
}
5056

5157
}

0 commit comments

Comments
 (0)