索引属性在mongo中的多个数据库中创建不必要的集合(SPRING)

时间:2018-07-11 15:13:46

标签: spring mongodb indexed

  • 在mongo中的多个数据库中建立索引的索引

@Indexed在两个数据库中都创建多个集合-第一个和第二个,而当我删除@Indexed属性时,不会创建任何集合。在这种情况下如何防止创建不必要的集合?

主要配置

@Configuration
@EnableMongoRepositories(basePackages = "com.marcosbarbero.wd.multiplemongo.repository.primary",
        mongoTemplateRef = "primaryTemplate")
public class PrimaryConfig extends AbstractMongoConfiguration {
    @Override
    protected String getDatabaseName() {
        return "first";
    }

    @Override
    public Mongo mongo() throws Exception {
        return new MongoClient("localhost", 27017);
    }

    @Override
    @Bean(name = "primaryTemplate")
    public MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(
                mongoDbFactory(),
                mappingMongoConverter());
    }
}

二级配置

@Configuration
@EnableMongoRepositories(basePackages = "com.marcosbarbero.wd.multiplemongo.repository.primary",
        mongoTemplateRef = "secondaryTemplate")
public class SecondaryConfig extends AbstractMongoConfiguration {
    @Override
    @Bean(name = "secondaryTemplateDb")
    protected String getDatabaseName() {
        return "second";
    }

    @Override
    public Mongo mongo() throws Exception {
        return new MongoClient("localhost", 27017);
    }

    @Override
    @Bean(name = "secondaryTemplate")
    public MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(
                mongoDbFactory(),
                mappingMongoConverter());
    }
}   

PrimaryModel

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "first_mongo")
public class PrimaryModel {

    @Id
    private String id;

    @Indexed
    private String value;

    @Override
    public String toString() {
        return "PrimaryModel{" + "id='" + id + '\'' + ", value='" + value + '\''
                + '}';
    }
}

SecondaryModel

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "second_mongo")
public class SecondaryModel {

    @Id
    private String id;

    private String value;

    @Override
    public String toString() {
        return "SecondaryModel{" + "id='" + id + '\'' + ", value='" + value + '\''
                + '}';
    }
}

应用

public class Application implements CommandLineRunner {

    @Autowired
    @Qualifier("primaryTemplate")
    private MongoOperations primaryRepository;

    @Autowired
    @Qualifier("secondaryTemplate")
    private MongoOperations secondaryRepository;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("************************************************************");
        log.info("Start printing mongo objects");
        log.info("************************************************************");
        Query query = new Query();
        primaryRepository.count(query, PrimaryModel.class);
        secondaryRepository.count(query, SecondaryModel.class);
    }
}

0 个答案:

没有答案