@Document 不会在 Spring Boot 应用程序中创建集合 [英] @Document doesn't create a collection in spring boot application

查看:75
本文介绍了@Document 不会在 Spring Boot 应用程序中创建集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 Spring Boot Rest 应用程序.尝试在 spring 数据 mongo db 中使用 @Document 注释创建一个集合.我知道如果文档用@Document 注释表示,spring 框架会创建一个集合.

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.

@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 控制器中,它会在插入命令时创建一个集合,但仍然不会在插入时创建文本索引.

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

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

推荐答案

我必须隐式设置才能工作

I have to set implicitly to work

在 application.properties 文件中

In application.properties file

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

或者application.yml文件

Or application.yml file

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

参考 - 请使用'MongoMappingContext#setAutoIndexCreation(boolean)'或覆盖'MongoConfigurationSupport#autoIndexCreation()' 明确

这篇关于@Document 不会在 Spring Boot 应用程序中创建集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆