0

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 ?

2
  • MongoTemplate has an insert method that takes a collection of objects to save, e.g.:mongoTemplate.insert(userList, "db_users"). Also, there is an insertAll method which can save to different collections based upon the class. Commented Jul 29, 2020 at 3:47
  • Thanks a lot @prasad_. Will try your suggestion. Commented Jul 29, 2020 at 11:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.