首页 > 解决方案 > ReactiveMongoTemplate .save 无法将@compondindex 注释添加到集合类

问题描述

我有一个 Java 类“A 类”,其对象以反应方式使用 reactiveMongoTemplate.save 操作保存在 mongoDB 中。

public class A {
 // field 1
 // field 2
 ..
 // field n
}

这一直工作得很好,并将大量数据填充到相应的集合中。

现在我决定为集合引入索引,因此将此注释添加到类中

@CompoundIndex(name ="collection_index_name", def = "{'field1':1, 'field1':-1}", unique=true)

现在添加此注释后,我看到我的 mongoDB 编写器(将数据写入上述 mongodb 集合的进程)长时间卡住(15-20 分钟)或没有进一步处理任何内容。在调试它时,我看到控制达到了我可以ReaciveMongoTemplate.save()操作的程度。但是在执行 save 反应方法之后,我只收到以下警告,并且集合没有发生写入。

Automatic index creation will be disabled by default as of Spring Data MongoDB 3.x.
    Please use 'MongoMappingContext#setAutoIndexCreation(boolean)' or override 'MongoConfigurationSupport#autoIndexCreation()' to be explicit.
    However, we recommend setting up indices manually in an application ready block. You may use index derivation there as well.

    > -----------------------------------------------------------------------------------------
    > @EventListener(ApplicationReadyEvent.class)
    > public void initIndicesAfterStartup() {
    >
    >     IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
    >
    >     IndexResolver resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
    >     resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
    > }
    > -----------------------------------------------------------------------------------------

一旦我删除上述注释或替换为新的空集合,并再次运行相同的代码,我就会看到条目立即被保存。

这种行为有什么解释吗?在集合中已经填充了大量数据之后,这与向集合添加索引有关吗?

标签: spring-bootproject-reactorspring-mongodbmongodb-indexes

解决方案


推荐阅读