title: How to Map a Java Entity to Multiple MongoDB Collections in Spring Data?
body: <p>Currently, we're looking for a solution to save the following <code>User</code> entity into multiple MongoDB collections at the same time (i.e. <code>db_users</code> and on <code>db_users_legacy</code>). Both collections are in the same database. </p>
  
  <p>Please don't ask me the reason why I need to save in two collections. It is a business requirement.</p>
  
  <pre><code>@Document(collection = "db_users")
  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  public class User {
  
      @Id
      private String id;
      private String name;
      private String website;
      private String name;
      private String email;
  
  }
  </code></pre>
  
  <p>And my SpringBoot application configuration goes as;</p>
  
  <pre><code>@Configuration
  public class ApplicationConfig {
  
      @Bean
      public MongoTemplate mongoTemplate(MongoDbFactory factory){
          MongoTemplate template = new MongoTemplate(factory);
          template.setWriteConcern(WriteConcern.ACKNOWLEDGED);
          retu,rn template;
      }
  
  }
  </code></pre>
  
  <p>Currently my repository looks as this. And saving works perfectly fine. How can I same this document in two different collections?</p>
  
  <pre><code>@Repository
  public class UserRepositoryImpl implements UserRepository {
  
      private MongoTemplate mongoTemplate;
  
      public UserRepositoryImpl(MongoTemplate mongoTemplate) {
          this.mongoTemplate = mongoTemplate;
      }
  
      @Override
      public void save(User user) {
          mongoTemplate.save(user);
      }
  
  }
  </code></pre>
  
  <p>Can anyone suggest the best option to deal with this, please?</p>