Clement
Clement

Reputation: 4811

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"))
}

Upvotes: 6

Views: 4063

Answers (3)

shishira chandra
shishira chandra

Reputation: 1

If you are using ReactiveMongoRepository; you cannot do a .block() as suggested by Mukundan.

Upvotes: -1

Zon
Zon

Reputation: 19880

To make you stream terminal with subscribe() operation and to get the Mono result at the same time - split into two separate operations:

Mono<String> myEvent = eventRepository.save(Event(UUID.randomUUID().toString(), "hey"));
myEvent.subscribe();
return myEvent;

Upvotes: 0

Gaurav Srivastav
Gaurav Srivastav

Reputation: 2551

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.

Upvotes: 12

Related Questions