输出结果如下:
Case 1...
User [id=1111, firstname=user, lastname=A, age=99]
Case 2...
User [id=2222, firstname=user, lastname=B, age=99]
Case 3...
User [id=3333, firstname=user, lastname=C, age=99]
User [id=4444, firstname=user, lastname=D, age=99]
User [id=5555, firstname=user, lastname=E, age=99]
User [id=1111, firstname=user, lastname=A, age=99]
Case 2...
User [id=2222, firstname=user, lastname=B, age=99]
Case 3...
User [id=3333, firstname=user, lastname=C, age=99]
User [id=4444, firstname=user, lastname=D, age=99]
User [id=5555, firstname=user, lastname=E, age=99]
更新Document
在mongodb中,可以使用save,updateFirst(),updateMulti()方法来进行更新,下面
是相关的例子
public class App {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(
SpringMongoConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx
.getBean("mongoTemplate");
User user = new User("1000", "user-first", "user-last", 17);
System.out.println("Case 1...by save()");
mongoOperation.save(user);
User userPrint1 = mongoOperation.findOne(new Query(Criteria.where("id").is("1000")), User.class);
System.out.println(userPrint1);
//修改user对象的lastname
user.setLastname("new last name");
//更新user对象
mongoOperation.save(user);
User userPrint2 = mongoOperation.findOne(new Query(Criteria.where("id")
.is("1000")), User.class);
System.out.println(userPrint2);
// Case 2 ... update firstname field, $set
System.out.println("Case 2...by updateFirst() - $set");
//将id为1000的user对象的firstname属性的值更新为”new firstname”
mongoOperation.updateFirst("user",
new Query(Criteria.where("_id").is("1000")),
Update.update("firstname", "new first name"));
User userPrint3 = mongoOperation.findOne(new Query(Criteria.where("id")
.is("1000")), User.class);
System.out.println(userPrint3);
//对id为1000的user的age加上10
System.out.println("Case 3...by updateFirst() - $inc");
Update updateAge = new Update();
updateAge.inc("age", 10);
mongoOperation.updateFirst("user",
new Query(Criteria.where("_id").is("1000")), updateAge);
User userPrint4 = mongoOperation.findOne(new Query(Criteria
.where("_id").is("1000")), User.class);
System.out.println(userPrint4);
}
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(
SpringMongoConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx
.getBean("mongoTemplate");
User user = new User("1000", "user-first", "user-last", 17);
System.out.println("Case 1...by save()");
mongoOperation.save(user);
User userPrint1 = mongoOperation.findOne(new Query(Criteria.where("id").is("1000")), User.class);
System.out.println(userPrint1);
//修改user对象的lastname
user.setLastname("new last name");
//更新user对象
mongoOperation.save(user);
User userPrint2 = mongoOperation.findOne(new Query(Criteria.where("id")
.is("1000")), User.class);
System.out.println(userPrint2);
// Case 2 ... update firstname field, $set
System.out.println("Case 2...by updateFirst() - $set");
//将id为1000的user对象的firstname属性的值更新为”new firstname”
mongoOperation.updateFirst("user",
new Query(Criteria.where("_id").is("1000")),
Update.update("firstname", "new first name"));
User userPrint3 = mongoOperation.findOne(new Query(Criteria.where("id")
.is("1000")), User.class);
System.out.println(userPrint3);
//对id为1000的user的age加上10
System.out.println("Case 3...by updateFirst() - $inc");
Update updateAge = new Update();
updateAge.inc("age", 10);
mongoOperation.updateFirst("user",
new Query(Criteria.where("_id").is("1000")), updateAge);
User userPrint4 = mongoOperation.findOne(new Query(Criteria
.where("_id").is("1000")), User.class);
System.out.println(userPrint4);
}
}
结果为:
Case 1...by save()
User [id=1000, firstname=user-first, lastname=user-last, age=17]
User [id=1000, firstname=user-first, lastname=new last name, age=17]
Case 2...by updateFirst() - $set
User [id=1000, firstname=new first name, lastname=new last name, age=17]
Case 3...by updateFirst() - $inc
User [id=1000, firstname=new first name, lastname=new last name, age=27]
User [id=1000, firstname=user-first, lastname=user-last, age=17]
User [id=1000, firstname=user-first, lastname=new last name, age=17]
Case 2...by updateFirst() - $set
User [id=1000, firstname=new first name, lastname=new last name, age=17]
Case 3...by updateFirst() - $inc
User [id=1000, firstname=new first name, lastname=new last name, age=27]