Currently, we're looking for a solution to save the List of User entity into multiple MongoDB collections at the same time (i.e. db_users and on db_users_legacy). Both collections are in the same database.
I have followed this link. In this link they are saving single User entity into two different collections. But , I want to insert list of User entity into two different collections. I tried to insert using for loop. But it is taking time to iterate entire list and save object one by one.
@Override
public void saveUser(List<User> userList) {
for(User user: userList) {
mongoTemplate.save(user, "db_users");
mongoTemplate.save(user, "db_users_legacy");
}
}
Is there any way to insert List of User entity into two different collections at one shot without using any for loop ?
MongoTemplatehas aninsertmethod that takes a collection of objects to save, e.g.:mongoTemplate.insert(userList, "db_users"). Also, there is aninsertAllmethod which can save to different collections based upon the class.