Search in sources :

Example 36 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project micronaut-data by micronaut-projects.

the class DefaultReactiveMongoRepositoryOperations method findOne.

@Override
public <T> Mono<T> findOne(Class<T> type, Serializable id) {
    return withClientSession(clientSession -> {
        RuntimePersistentEntity<T> persistentEntity = runtimeEntityRegistry.getEntity(type);
        MongoDatabase database = getDatabase(persistentEntity, null);
        MongoCollection<T> collection = getCollection(database, persistentEntity, type);
        Bson filter = MongoUtils.filterById(conversionService, persistentEntity, id, collection.getCodecRegistry());
        if (QUERY_LOG.isDebugEnabled()) {
            QUERY_LOG.debug("Executing Mongo 'find' with filter: {}", filter.toBsonDocument().toJson());
        }
        return Mono.from(collection.find(clientSession, filter, type).first());
    });
}
Also used : MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Bson(org.bson.conversions.Bson)

Example 37 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project micronaut-data by micronaut-projects.

the class DefaultReactiveMongoRepositoryOperations method persistManyAssociation.

@Override
public Mono<Void> persistManyAssociation(MongoOperationContext ctx, RuntimeAssociation runtimeAssociation, Object value, RuntimePersistentEntity<Object> persistentEntity, Object child, RuntimePersistentEntity<Object> childPersistentEntity) {
    String joinCollectionName = runtimeAssociation.getOwner().getNamingStrategy().mappedName(runtimeAssociation);
    MongoDatabase mongoDatabase = getDatabase(persistentEntity, ctx.repositoryType);
    MongoCollection<BsonDocument> collection = mongoDatabase.getCollection(joinCollectionName, BsonDocument.class);
    BsonDocument association = association(collection.getCodecRegistry(), value, persistentEntity, child, childPersistentEntity);
    if (QUERY_LOG.isDebugEnabled()) {
        QUERY_LOG.debug("Executing Mongo 'insertOne' for collection: {} with document: {}", collection.getNamespace().getFullName(), association);
    }
    return Mono.from(collection.insertOne(ctx.clientSession, association)).then();
}
Also used : BsonDocument(org.bson.BsonDocument) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase)

Example 38 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project pinpoint by naver.

the class MongoReactivePluginController method insertOn.

@GetMapping(value = "/mongodb/insertOne")
public String insertOn() throws Throwable {
    MongoDatabase db = getDatabase();
    MongoCollection<Document> collection = db.getCollection("test");
    Document canvas = new Document("item", "canvas").append("qty", 100).append("tags", Collections.singletonList("cotton"));
    Document size = new Document("h", 28).append("w", 35.5).append("uom", "cm");
    canvas.put("size", size);
    ObservableSubscriber<InsertOneResult> sub = new ObservableSubscriber<>();
    collection.insertOne(canvas).subscribe(sub);
    sub.waitForThenCancel(1);
    return "Insert=" + sub.getResults();
}
Also used : Document(org.bson.Document) InsertOneResult(com.mongodb.client.result.InsertOneResult) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)38 Document (org.bson.Document)15 MongoClient (com.mongodb.reactivestreams.client.MongoClient)9 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)8 DeleteResult (com.mongodb.client.result.DeleteResult)6 List (java.util.List)6 Map (java.util.Map)6 HashMap (java.util.HashMap)5 BsonDocument (org.bson.BsonDocument)5 MongoClientSettings (com.mongodb.MongoClientSettings)4 MongoException (com.mongodb.MongoException)4 InsertOneResult (com.mongodb.client.result.InsertOneResult)4 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)4 Publisher (org.reactivestreams.Publisher)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 WriteConcern (com.mongodb.WriteConcern)3 DeleteOneModel (com.mongodb.client.model.DeleteOneModel)3 FindPublisher (com.mongodb.reactivestreams.client.FindPublisher)3 MongoClients (com.mongodb.reactivestreams.client.MongoClients)3