@Document doesn't create a collection in spring boot application

假如想象 提交于 2021-01-29 06:33:48

问题


I have a simple spring boot rest application. Trying to create a collection using @Document annotation in spring data mongo db. I know spring framework creates a collection if the document is denoted with @Document annotation.

Entity

@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;
    }
}

properties

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

However, in the rest controller, it creates a collection on insert command, but still, it doesn't create a Text-index on insert.

@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";
    }
}

Don't have any error in the console as well

Console

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)

Code repository

https://github.com/anandjaisy/mongoDBSpringBoot

回答1:


I have to set implicitly to work

In application.properties file

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

Or application.yml file

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

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



来源:https://stackoverflow.com/questions/63708853/document-doesnt-create-a-collection-in-spring-boot-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!