输出结果如下:
savedUser : User [id=1001, firstname=yong, lastname=mook kim, age=30]
updatedUser : User [id=1001, firstname=yong, lastname=new lastname, age=30]
Number of user = 0
updatedUser : User [id=1001, firstname=yong, lastname=new lastname, age=30]
Number of user = 0
Spring mongodb插入数据
下面详细讲解如何使用spring mongodb插入数据。在spring mongodb中,插入数据到
mongodb有如下几种方法:
User user = new User("...");
//将user对象保存到"user"这个collection中
mongoOperation.save(user);
//将user对象保存到"new collection"这个collection中
mongoOperation.save("new collection",user);
//将user对象保存到"user"这个collection中
mongoOperation.insert(user);
//将user对象保存到"new collection"这个collection中
mongoOperation.insert("new collection", user);
//将user的对象列表(List)保存到"user"collection中去
mongoOperation.insertList(userInList);
//将user的对象列表(List)保存到"new collection"collection中去
mongoOperation.insertList("new collection", userInList);
//将user对象保存到"user"这个collection中
mongoOperation.save(user);
//将user对象保存到"new collection"这个collection中
mongoOperation.save("new collection",user);
//将user对象保存到"user"这个collection中
mongoOperation.insert(user);
//将user对象保存到"new collection"这个collection中
mongoOperation.insert("new collection", user);
//将user的对象列表(List)保存到"user"collection中去
mongoOperation.insertList(userInList);
//将user的对象列表(List)保存到"new collection"collection中去
mongoOperation.insertList("new collection", userInList);
要注意的是,Spring mongodb中,当没有指定collection时,就会把对象保存到以对象命名的collection中。比如上例中的mongoOperation.insert(user),由于没指定collection的名称,所以会把user对象保存到user这个新建立的collection中。
另外请注意其中的save和insert的区别。它们的区别为:
1)save意思是,当记录不存在时插入,或者是当记录已存在是更新,实际上就是saveorupdate的意思。
2) insert的意思是:当记录不存在时插入,而如果记录存在时则忽略,继续插入。
下面举例子说明:
package com.mkyong.core;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
import com.mkyong.config.SpringMongoConfig;
import com.mkyong.user.User;
public class App {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(
SpringMongoConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx
.getBean("mongoTemplate");
//新增一个user对象,并把它放到"ABC"这个collection中
System.out.println("Case 1...");
User userA = new User("1111", "user", "A", 99);
mongoOperation.save("ABC", userA);
// 查找刚插入的user对象
User userA1 = mongoOperation.findOne("ABC",
new Query(Criteria.where("id").is("1111")), User.class);
System.out.println(userA1);
//插入新的user,放到userB这个collection中去
System.out.println("Case 2...");
User userB = new User("2222", "user", "B", 99);
mongoOperation.save(userB);
// 查找
User userB1 = mongoOperation.findOne(
new Query(Criteria.where("id").is("2222")), User.class);
System.out.println(userB1);
// 插入对象列表,放到arraylist中
System.out.println("Case 3...");
User userC = new User("3333", "user", "C", 99);
User userD = new User("4444", "user", "D", 99);
User userE = new User("5555", "user", "E", 99);
List<User> userList = new ArrayList<User>();
userList.add(userC);
userList.add(userD);
userList.add(userE);
mongoOperation.insertList("ABC-List", userList);
List<User> users = mongoOperation.find("ABC-List", new Query(Criteria
.where("firstname").is("user")), User.class);
for (User temp : users) {
System.out.println(temp);
}
}
}
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
import com.mkyong.config.SpringMongoConfig;
import com.mkyong.user.User;
public class App {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(
SpringMongoConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx
.getBean("mongoTemplate");
//新增一个user对象,并把它放到"ABC"这个collection中
System.out.println("Case 1...");
User userA = new User("1111", "user", "A", 99);
mongoOperation.save("ABC", userA);
// 查找刚插入的user对象
User userA1 = mongoOperation.findOne("ABC",
new Query(Criteria.where("id").is("1111")), User.class);
System.out.println(userA1);
//插入新的user,放到userB这个collection中去
System.out.println("Case 2...");
User userB = new User("2222", "user", "B", 99);
mongoOperation.save(userB);
// 查找
User userB1 = mongoOperation.findOne(
new Query(Criteria.where("id").is("2222")), User.class);
System.out.println(userB1);
// 插入对象列表,放到arraylist中
System.out.println("Case 3...");
User userC = new User("3333", "user", "C", 99);
User userD = new User("4444", "user", "D", 99);
User userE = new User("5555", "user", "E", 99);
List<User> userList = new ArrayList<User>();
userList.add(userC);
userList.add(userD);
userList.add(userE);
mongoOperation.insertList("ABC-List", userList);
List<User> users = mongoOperation.find("ABC-List", new Query(Criteria
.where("firstname").is("user")), User.class);
for (User temp : users) {
System.out.println(temp);
}
}
}