In Spring data for MongoDB, you can use save(), insert() and insertList() to save domain object into mongoDB database.

	User user = new User("...");
 
	//save user object into "user" collection
	mongoOperation.save(user);
 
	//save user object into "new collection" collection
	mongoOperation.save("new collection",user);
 
	//save user object into "user" collection
	mongoOperation.insert(user);
 
	//save user object into "new collection" collection
	mongoOperation.insert("new collection", user);
 
	//save list of user objects into "user" collection
	mongoOperation.insertList(userInList);
 
	//save list of user objects into "new collection" collection
	mongoOperation.insertList("new collection", userInList);

By default, if you saved an object, and didn’t define any of the “collection name”, it will use the domain object name as the collection name. See the “save” method on mongoTemplate.

public void save(Object objectToSave) {
	save(getEntityCollection(objectToSave), objectToSave);
}
save or insert ?

  1. “save” is means “insert it if record is not exists” and “update it if record is existed”, or simply saveOrUpdate().
  2. “insert” is means “insert it if record is not exits” and “ignore it if record is existed”.

Insert documents example

See a full example to show the use of “Spring Data MongoDB” APIs to save user object into MongoDB.

public class User {
 
	private String id;
	private String firstname;
	private String lastname;
	private int age;
 
       //getter, setters and constructors 
}
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) {
		// For Annotation
		ApplicationContext ctx = new AnnotationConfigApplicationContext(
				SpringMongoConfig.class);
		MongoOperations mongoOperation = (MongoOperations) ctx
				.getBean("mongoTemplate");
 
		// Case1 - insert a user, put "ABC" as collection name
		System.out.println("Case 1...");
		User userA = new User("1111", "user", "A", 99);
		mongoOperation.save("ABC", userA);
 
		// find
		User userA1 = mongoOperation.findOne("ABC",
				new Query(Criteria.where("id").is("1111")), User.class);
 
		System.out.println(userA1);
 
		// Case2 - insert a user, put entity as collection name
		System.out.println("Case 2...");
		User userB = new User("2222", "user", "B", 99);
		mongoOperation.save(userB);
 
		// find
		User userB1 = mongoOperation.findOne(
				new Query(Criteria.where("id").is("2222")), User.class);
 
		System.out.println(userB1);
 
		// Case3 - insert a list of users
		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);
 
		// find
		List<User> users = mongoOperation.find("ABC-List", new Query(Criteria
				.where("firstname").is("user")), User.class);
 
		for (User temp : users) {
			System.out.println(temp);
		}
 
	}
 
}

Output

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]
Note : You can find more similar articles at - Java MongoDB Tutorials