Search code examples
springmongodbkotlinspring-data-mongodb

Spring Reactive MongoDB not saving document


I'm trying to save a basic document but despite connecting to mongodb successfully... It doesn't seem to want to save.

Spring logs

2018-10-03 00:17:25.998  INFO 10713 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2018-10-03 00:17:26.049  INFO 10713 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-10-03 00:17:26.106  INFO 10713 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext     : Started HttpServer on /0:0:0:0:0:0:0:0:8080
2018-10-03 00:17:26.106  INFO 10713 --- [  restartedMain] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8080
2018-10-03 00:17:26.112  INFO 10713 --- [  restartedMain] c.l.s.ServiceLegalApplicationKt          : Started ServiceLegalApplicationKt in 3.459 seconds (JVM running for 4.201)
2018-10-03 00:17:26.644  INFO 10713 --- [ntLoopGroup-2-2] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:4}] to localhost:27017

application.properties

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=legal
spring.data.mongodb.repositories.type=reactive
spring.mongodb.embedded.version=4.0.2

basic interface and class

interface EventRepository: ReactiveMongoRepository<Event, String>

@Document
class Event(id: String, name: String)

trying a simple save function

@Service
class SomeService(val eventRepository: EventRepository)
{
    fun save() = eventRepository.save(Event(UUID.randomUUID().toString(), "hey"))
}

Solution

  • Mono<Event> response = repository.save(Event(UUID.randomUUID().toString(), "hey"));
    

    Changes in save method

    fun save() = eventRepository.save(Event(UUID.randomUUID().toString(), "hey")).subscribe();
    

    You have to invoke subscribe() method on Mono reference to see the logs or details.