Search in sources :

Example 16 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project mongo-java-driver by mongodb.

the class PojoQuickTour method main.

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes an optional single argument for the connection string
 */
public static void main(final String[] args) {
    MongoClient mongoClient;
    if (args.length == 0) {
        // connect to the local database server
        mongoClient = MongoClients.create();
    } else {
        mongoClient = MongoClients.create(args[0]);
    }
    // create codec registry for POJOs
    CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build()));
    // get handle to "mydb" database
    MongoDatabase database = mongoClient.getDatabase("mydb").withCodecRegistry(pojoCodecRegistry);
    // get a handle to the "people" collection
    final MongoCollection<Person> collection = database.getCollection("people", Person.class);
    // drop all the data in it
    ObservableSubscriber<Void> successSubscriber = new OperationSubscriber<>();
    collection.drop().subscribe(successSubscriber);
    successSubscriber.await();
    // make a document and insert it
    final Person ada = new Person("Ada Byron", 20, new Address("St James Square", "London", "W1"));
    System.out.println("Original Person Model: " + ada);
    ObservableSubscriber<InsertOneResult> insertOneSubscriber = new OperationSubscriber<>();
    collection.insertOne(ada).subscribe(insertOneSubscriber);
    insertOneSubscriber.await();
    // get it (since it's the only one in there since we dropped the rest earlier on)
    ObservableSubscriber<Person> personSubscriber = new PrintToStringSubscriber<>();
    collection.find().first().subscribe(personSubscriber);
    personSubscriber.await();
    // now, lets add some more people so we can explore queries and cursors
    List<Person> people = asList(new Person("Charles Babbage", 45, new Address("5 Devonshire Street", "London", "W11")), new Person("Alan Turing", 28, new Address("Bletchley Hall", "Bletchley Park", "MK12")), new Person("Timothy Berners-Lee", 61, new Address("Colehill", "Wimborne", null)));
    ObservableSubscriber<InsertManyResult> insertManySubscriber = new OperationSubscriber<>();
    collection.insertMany(people).subscribe(insertManySubscriber);
    insertManySubscriber.await();
    // get all the documents in the collection and print them out
    personSubscriber = new PrintToStringSubscriber<>();
    collection.find().subscribe(personSubscriber);
    personSubscriber.await();
    // now use a query to get 1 document out
    personSubscriber = new PrintToStringSubscriber<>();
    collection.find(eq("address.city", "Wimborne")).first().subscribe(personSubscriber);
    personSubscriber.await();
    // now lets find every over 30
    personSubscriber = new PrintToStringSubscriber<>();
    collection.find(gt("age", 30)).subscribe(personSubscriber);
    personSubscriber.await();
    // Update One
    ObservableSubscriber<UpdateResult> updateSubscriber = new OperationSubscriber<>();
    collection.updateOne(eq("name", "Ada Byron"), combine(set("age", 23), set("name", "Ada Lovelace"))).subscribe(updateSubscriber);
    updateSubscriber.await();
    // Update Many
    updateSubscriber = new OperationSubscriber<>();
    collection.updateMany(not(eq("zip", null)), set("zip", null)).subscribe(updateSubscriber);
    updateSubscriber.await();
    // Replace One
    updateSubscriber = new OperationSubscriber<>();
    collection.replaceOne(eq("name", "Ada Lovelace"), ada).subscribe(updateSubscriber);
    updateSubscriber.await();
    // Delete One
    ObservableSubscriber<DeleteResult> deleteSubscriber = new OperationSubscriber<>();
    collection.deleteOne(eq("address.city", "Wimborne")).subscribe(deleteSubscriber);
    deleteSubscriber.await();
    // Delete Many
    deleteSubscriber = new OperationSubscriber<>();
    collection.deleteMany(eq("address.city", "London")).subscribe(deleteSubscriber);
    deleteSubscriber.await();
    // Clean up
    successSubscriber = new OperationSubscriber<>();
    database.drop().subscribe(successSubscriber);
    successSubscriber.await();
    // release resources
    mongoClient.close();
}
Also used : OperationSubscriber(reactivestreams.helpers.SubscriberHelpers.OperationSubscriber) InsertManyResult(com.mongodb.client.result.InsertManyResult) MongoClient(com.mongodb.reactivestreams.client.MongoClient) InsertOneResult(com.mongodb.client.result.InsertOneResult) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) PrintToStringSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintToStringSubscriber) UpdateResult(com.mongodb.client.result.UpdateResult) DeleteResult(com.mongodb.client.result.DeleteResult) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase)

Example 17 with MongoDatabase

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

the class ReactiveQuerydslMongoPredicateExecutorTests method translatesExceptionsCorrectly.

// DATAMONGO-2182, DATAMONGO-2265
@Test
public void translatesExceptionsCorrectly() {
    ReactiveMongoOperations ops = new ReactiveMongoTemplate(dbFactory) {

        @Override
        protected Mono<MongoDatabase> doGetDatabase() {
            return Mono.error(new MongoException(18, "Authentication Failed"));
        }
    };
    ReactiveMongoRepositoryFactory factory = new ReactiveMongoRepositoryFactory(ops);
    MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
    repository = new ReactiveQuerydslMongoPredicateExecutor<>(entityInformation, ops);
    // 
    repository.findOne(person.firstname.contains("batman")).as(// 
    StepVerifier::create).expectError(// 
    PermissionDeniedDataAccessException.class).verify();
}
Also used : ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) MongoException(com.mongodb.MongoException) ReactiveMongoOperations(org.springframework.data.mongodb.core.ReactiveMongoOperations) PermissionDeniedDataAccessException(org.springframework.dao.PermissionDeniedDataAccessException) StepVerifier(reactor.test.StepVerifier) Person(org.springframework.data.mongodb.repository.Person) QPerson(org.springframework.data.mongodb.repository.QPerson) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Test(org.junit.Test)

Example 18 with MongoDatabase

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

the class ReactivePerformanceTests method setupCollections.

private void setupCollections() {
    MongoDatabase db = this.mongo.getDatabase(DATABASE_NAME);
    for (String collectionName : COLLECTION_NAMES) {
        MongoCollection<Document> collection = db.getCollection(collectionName);
        Mono.from(collection.drop()).block();
        Mono.from(db.createCollection(collectionName, getCreateCollectionOptions())).block();
        collection.createIndex(new BasicDBObject("firstname", -1));
        collection.createIndex(new BasicDBObject("lastname", -1));
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Document(org.bson.Document) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase)

Example 19 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project MongoSK by Romitou.

the class ExprMongoDatabase method get.

@Override
protected MongoSKDatabase[] get(@Nonnull final Event e) {
    String databaseName = exprDatabaseName.getSingle(e);
    MongoSKServer mongoSKServer = exprMongoSKServer.getSingle(e);
    if (databaseName == null || mongoSKServer == null)
        return new MongoSKDatabase[0];
    MongoDatabase mongoDatabase = mongoSKServer.getMongoClient().getDatabase(databaseName);
    if (mongoDatabase == null)
        return new MongoSKDatabase[0];
    return new MongoSKDatabase[] { new MongoSKDatabase(mongoDatabase) };
}
Also used : MongoSKServer(fr.romitou.mongosk.elements.MongoSKServer) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) MongoSKDatabase(fr.romitou.mongosk.elements.MongoSKDatabase)

Example 20 with MongoDatabase

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

the class SimpleReactiveMongoDatabaseFactory method getMongoDatabase.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.ReactiveMongoDbFactory#getMongoDatabase(java.lang.String)
	 */
public MongoDatabase getMongoDatabase(String dbName) throws DataAccessException {
    Assert.hasText(dbName, "Database name must not be empty.");
    MongoDatabase db = mongo.getDatabase(dbName);
    return writeConcern != null ? db.withWriteConcern(writeConcern) : db;
}
Also used : MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase)

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