【问题标题】:@Document doesn't create a collection in spring boot application@Document 不在 Spring Boot 应用程序中创建集合
【发布时间】:2020-12-21 19:39:18
【问题描述】:

我有一个简单的 spring boot rest 应用程序。尝试在 spring data mongo db 中使用 @Document 注释创建一个集合。我知道如果文档用@Document 注释表示,spring 框架会创建一个集合。

实体

@Document("User")
public class User {
    @Id
    private String Id;
    @Field("firstName")
    @TextIndexed
    private String firstName;
    @Field("lastName")
    @TextIndexed
    private String lastName;
    private String address;

    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

属性

spring.data.mongodb.uri=mongodb://localhost:27017/Order

但是,在 rest 控制器中,它会在 insert 命令时创建一个集合,但它仍然不会在 insert 时创建一个 Text-index。

@RestController
public class Controller {
    private MongoTemplate mongoTemplate;

    public Controller(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    @GetMapping("/get")
    public String Get(){
        mongoTemplate.insert(new User());
        return "HelloWorld";
    }
}

控制台也没有任何错误

控制台

2020-09-03 12:52:00.657  INFO 865 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on macbooks-MacBook-Air.local with PID 865 (/Users/macbook/Projects/Fete/demo/build/classes/java/main started by macbook in /Users/macbook/Projects/Fete/demo)
2020-09-03 12:52:00.662  INFO 865 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-09-03 12:52:02.676  INFO 865 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2020-09-03 12:52:02.712  INFO 865 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 22ms. Found 0 MongoDB repository interfaces.
2020-09-03 12:52:04.106  INFO 865 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-09-03 12:52:04.136  INFO 865 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-09-03 12:52:04.137  INFO 865 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-09-03 12:52:04.269  INFO 865 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-09-03 12:52:04.270  INFO 865 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3500 ms
2020-09-03 12:52:04.558  INFO 865 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2020-09-03 12:52:04.692  INFO 865 --- [localhost:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:9}] to localhost:27017
2020-09-03 12:52:04.731  INFO 865 --- [localhost:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=8577619}
2020-09-03 12:52:06.165  INFO 865 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-03 12:52:06.746  INFO 865 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-09-03 12:52:06.764  INFO 865 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 6.69 seconds (JVM running for 13.238)

代码库

https://github.com/anandjaisy/mongoDBSpringBoot

【问题讨论】:

  • 你有什么错误吗?
  • 不,我没有收到任何错误
  • 你怎么打电话给mongoTemplate.insert(new User());
  • 我从其他 API 控制器调用,它插入一个空记录,但不创建文本索引
  • 你能给我们看看吗

标签: java spring spring-boot spring-data spring-mongodb


【解决方案1】:

我必须隐式设置才能工作

在 application.properties 文件中

spring.data.mongodb.auto-index-creation=true

或者application.yml文件

spring:
  data:
    mongodb:
      auto-index-creation: true

参考 - Please use 'MongoMappingContext#setAutoIndexCreation(boolean)' or override 'MongoConfigurationSupport#autoIndexCreation()' to be explicit

【讨论】:

    猜你喜欢
    • 2019-06-08
    • 2019-03-04
    • 2016-07-04
    • 2018-07-03
    • 2016-02-29
    • 2019-07-24
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    相关资源
    最近更新 更多