Currently, we're looking for a solution to save the following 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.
Please don't ask me the reason why I need to save in two collections. It is a business requirement.
@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;
}
And my SpringBoot application configuration goes as;
@Configuration
public class ApplicationConfig {
@Bean
public MongoTemplate mongoTemplate(MongoDbFactory factory){
MongoTemplate template = new MongoTemplate(factory);
template.setWriteConcern(WriteConcern.ACKNOWLEDGED);
retu,rn template;
}
}
Currently my repository looks as this. And saving works perfectly fine. How can I same this document in two different collections?
@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);
}
}
Can anyone suggest the best option to deal with this, please?