Search in sources :

Example 6 with PrintDocumentSubscriber

use of reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber in project mongo-java-driver by mongodb.

the class ClientSideEncryptionSimpleTour method main.

/**
 * Run this main method to see the output of this quick example.
 *
 * Requires the mongodb-crypt library in the class path and mongocryptd on the system path.
 * Assumes the schema has already been created in MongoDB.
 *
 * @param args ignored args
 */
public static void main(final String[] args) {
    // This would have to be the same master key as was used to create the encryption key
    final byte[] localMasterKey = new byte[96];
    new SecureRandom().nextBytes(localMasterKey);
    Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {

        {
            put("local", new HashMap<String, Object>() {

                {
                    put("key", localMasterKey);
                }
            });
        }
    };
    String keyVaultNamespace = "admin.datakeys";
    AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProviders).build();
    MongoClientSettings clientSettings = MongoClientSettings.builder().autoEncryptionSettings(autoEncryptionSettings).build();
    MongoClient mongoClient = MongoClients.create(clientSettings);
    MongoCollection<Document> collection = mongoClient.getDatabase("test").getCollection("coll");
    ObservableSubscriber<Void> successSubscriber = new OperationSubscriber<>();
    collection.drop().subscribe(successSubscriber);
    successSubscriber.await();
    ObservableSubscriber<InsertOneResult> insertOneSubscriber = new OperationSubscriber<>();
    collection.insertOne(new Document("encryptedField", "123456789")).subscribe(insertOneSubscriber);
    insertOneSubscriber.await();
    ObservableSubscriber<Document> documentSubscriber = new PrintDocumentSubscriber();
    collection.find().first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // release resources
    mongoClient.close();
}
Also used : PrintDocumentSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber) HashMap(java.util.HashMap) OperationSubscriber(reactivestreams.helpers.SubscriberHelpers.OperationSubscriber) SecureRandom(java.security.SecureRandom) MongoClientSettings(com.mongodb.MongoClientSettings) Document(org.bson.Document) MongoClient(com.mongodb.reactivestreams.client.MongoClient) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) InsertOneResult(com.mongodb.client.result.InsertOneResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with PrintDocumentSubscriber

use of reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber in project mongo-java-driver by mongodb.

the class QueryPrimer method lessThan.

@Test
public void lessThan() {
    // @begin: less-than
    // @code: start
    FindPublisher<Document> publisher = db.getCollection("restaurants").find(new Document("grades.score", new Document("$lt", 10)));
    // @code: end
    // @pre: Iterate the results and apply a block to each resulting document
    // @code: start
    ObservableSubscriber<Document> documentSubscriber = new PrintDocumentSubscriber();
    publisher.subscribe(documentSubscriber);
    documentSubscriber.await();
    // @code: end
    // @pre: To simplify building queries the Java driver provides static helpers
    // @code: start
    db.getCollection("restaurants").find(lt("grades.score", 10));
// @code: end
// @end: less-than
}
Also used : PrintDocumentSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber) Document(org.bson.Document) Test(org.junit.Test)

Example 8 with PrintDocumentSubscriber

use of reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber in project mongo-java-driver by mongodb.

the class QueryPrimer method queryFieldInArray.

@Test
public void queryFieldInArray() {
    // @begin: query-field-in-array
    // @code: start
    FindPublisher<Document> publisher = db.getCollection("restaurants").find(new Document("grades.grade", "B"));
    // @code: end
    // @pre: Iterate the results and apply a block to each resulting document
    // @code: start
    ObservableSubscriber<Document> documentSubscriber = new PrintDocumentSubscriber();
    publisher.subscribe(documentSubscriber);
    documentSubscriber.await();
    // @code: end
    // @pre: To simplify building queries the Java driver provides static helpers
    // @code: start
    db.getCollection("restaurants").find(eq("grades.grade", "B"));
// @code: end
// @end: query-field-in-array
}
Also used : PrintDocumentSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber) Document(org.bson.Document) Test(org.junit.Test)

Example 9 with PrintDocumentSubscriber

use of reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber in project mongo-java-driver by mongodb.

the class QueryPrimer method logicalOr.

@Test
public void logicalOr() {
    // @begin: logical-or
    // @code: start
    FindPublisher<Document> publisher = db.getCollection("restaurants").find(new Document("$or", asList(new Document("cuisine", "Italian"), new Document("address.zipcode", "10075"))));
    // @code: end
    // @pre: Iterate the results and apply a block to each resulting document
    // @code: start
    ObservableSubscriber<Document> documentSubscriber = new PrintDocumentSubscriber();
    publisher.subscribe(documentSubscriber);
    documentSubscriber.await();
    // @code: end
    // @pre: To simplify building queries the Java driver provides static helpers
    // @code: start
    db.getCollection("restaurants").find(or(eq("cuisine", "Italian"), eq("address.zipcode", "10075")));
// @code: end
// @end: logical-or
}
Also used : PrintDocumentSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber) Document(org.bson.Document) Test(org.junit.Test)

Aggregations

Document (org.bson.Document)9 PrintDocumentSubscriber (reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber)9 Test (org.junit.Test)6 InsertOneResult (com.mongodb.client.result.InsertOneResult)3 MongoClient (com.mongodb.reactivestreams.client.MongoClient)3 OperationSubscriber (reactivestreams.helpers.SubscriberHelpers.OperationSubscriber)3 AutoEncryptionSettings (com.mongodb.AutoEncryptionSettings)2 MongoClientSettings (com.mongodb.MongoClientSettings)2 SecureRandom (java.security.SecureRandom)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)1 ConnectionString (com.mongodb.ConnectionString)1 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)1 DeleteResult (com.mongodb.client.result.DeleteResult)1 InsertManyResult (com.mongodb.client.result.InsertManyResult)1 UpdateResult (com.mongodb.client.result.UpdateResult)1 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)1 ClientEncryption (com.mongodb.reactivestreams.client.vault.ClientEncryption)1 ArrayList (java.util.ArrayList)1