use of reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber in project mongo-java-driver by mongodb.
the class QueryPrimer method greaterThan.
@Test
public void greaterThan() {
// @begin: greater-than
// @code: start
FindPublisher<Document> publisher = db.getCollection("restaurants").find(new Document("grades.score", new Document("$gt", 30)));
// @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(gt("grades.score", 30));
// @code: end
// @end: greater-than
}
use of reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber in project mongo-java-driver by mongodb.
the class QueryPrimer method queryTopLevelField.
@Test
public void queryTopLevelField() {
// @begin: query-top-level-field
// @code: start
FindPublisher<Document> publisher = db.getCollection("restaurants").find(new Document("borough", "Manhattan"));
// @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("borough", "Manhattan"));
// @code: end
// @end: query-top-level-field
}
use of reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber in project mongo-java-driver by mongodb.
the class QueryPrimer method queryEmbeddedDocument.
@Test
public void queryEmbeddedDocument() {
// @begin: query-embedded-document
// @code: start
FindPublisher<Document> publisher = db.getCollection("restaurants").find(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(eq("address.zipcode", "10075"));
// @code: end
// @end: query-embedded-document
}
use of reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber in project mongo-java-driver by mongodb.
the class ClientSideEncryptionAutoEncryptionSettingsTour 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.
*
* @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";
ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder().keyVaultMongoClientSettings(MongoClientSettings.builder().applyConnectionString(new ConnectionString("mongodb://localhost")).build()).keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProviders).build();
ClientEncryption clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
ObservableSubscriber<BsonBinary> dataKeySubscriber = new OperationSubscriber<>();
clientEncryption.createDataKey("local", new DataKeyOptions()).subscribe(dataKeySubscriber);
dataKeySubscriber.await();
String base64DataKeyId = Base64.getEncoder().encodeToString(dataKeySubscriber.getReceived().get(0).getData());
final String dbName = "test";
final String collName = "coll";
AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProviders).schemaMap(new HashMap<String, BsonDocument>() {
{
put(dbName + "." + collName, // Need a schema that references the new data key
BsonDocument.parse("{" + " properties: {" + " encryptedField: {" + " encrypt: {" + " keyId: [{" + " \"$binary\": {" + " \"base64\": \"" + base64DataKeyId + "\"," + " \"subType\": \"04\"" + " }" + " }]," + " bsonType: \"string\"," + " algorithm: \"AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic\"" + " }" + " }" + " }," + " \"bsonType\": \"object\"" + "}"));
}
}).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();
}
use of reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber in project mongo-java-driver by mongodb.
the class QuickTour 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]);
}
// get handle to "mydb" database
MongoDatabase database = mongoClient.getDatabase("mydb");
// get a handle to the "test" collection
final MongoCollection<Document> collection = database.getCollection("test");
// drop all the data in it
ObservableSubscriber<Void> successSubscriber = new OperationSubscriber<>();
collection.drop().subscribe(successSubscriber);
successSubscriber.await();
// make a document and insert it
Document doc = new Document("name", "MongoDB").append("type", "database").append("count", 1).append("info", new Document("x", 203).append("y", 102));
ObservableSubscriber<InsertOneResult> insertOneSubscriber = new OperationSubscriber<>();
collection.insertOne(doc).subscribe(insertOneSubscriber);
insertOneSubscriber.await();
// get it (since it's the only one in there since we dropped the rest earlier on)
ObservableSubscriber<Document> documentSubscriber = new PrintDocumentSubscriber();
collection.find().first().subscribe(documentSubscriber);
documentSubscriber.await();
// now, lets add lots of little documents to the collection so we can explore queries and cursors
List<Document> documents = new ArrayList<>();
for (int i = 0; i < 100; i++) {
documents.add(new Document("i", i));
}
ObservableSubscriber<InsertManyResult> insertManySubscriber = new OperationSubscriber<>();
collection.insertMany(documents).subscribe(insertManySubscriber);
insertManySubscriber.await();
// find first
documentSubscriber = new PrintDocumentSubscriber();
collection.find().first().subscribe(documentSubscriber);
documentSubscriber.await();
// lets get all the documents in the collection and print them out
documentSubscriber = new PrintDocumentSubscriber();
collection.find().subscribe(documentSubscriber);
documentSubscriber.await();
// Query Filters
// now use a query to get 1 document out
documentSubscriber = new PrintDocumentSubscriber();
collection.find(eq("i", 71)).first().subscribe(documentSubscriber);
documentSubscriber.await();
// now use a range query to get a larger subset
documentSubscriber = new PrintDocumentSubscriber();
collection.find(gt("i", 50)).subscribe(documentSubscriber);
successSubscriber.await();
// range query with multiple constraints
documentSubscriber = new PrintDocumentSubscriber();
collection.find(and(gt("i", 50), lte("i", 100))).subscribe(documentSubscriber);
successSubscriber.await();
// Sorting
documentSubscriber = new PrintDocumentSubscriber();
collection.find(exists("i")).sort(descending("i")).first().subscribe(documentSubscriber);
documentSubscriber.await();
// Projection
documentSubscriber = new PrintDocumentSubscriber();
collection.find().projection(excludeId()).first().subscribe(documentSubscriber);
documentSubscriber.await();
// Aggregation
documentSubscriber = new PrintDocumentSubscriber();
collection.aggregate(asList(match(gt("i", 0)), project(Document.parse("{ITimes10: {$multiply: ['$i', 10]}}")))).subscribe(documentSubscriber);
documentSubscriber.await();
documentSubscriber = new PrintDocumentSubscriber();
collection.aggregate(singletonList(group(null, sum("total", "$i")))).first().subscribe(documentSubscriber);
documentSubscriber.await();
// Update One
ObservableSubscriber<UpdateResult> updateSubscriber = new OperationSubscriber<>();
collection.updateOne(eq("i", 10), set("i", 110)).subscribe(updateSubscriber);
updateSubscriber.await();
// Update Many
updateSubscriber = new OperationSubscriber<>();
collection.updateMany(lt("i", 100), inc("i", 100)).subscribe(updateSubscriber);
updateSubscriber.await();
// Delete One
ObservableSubscriber<DeleteResult> deleteSubscriber = new OperationSubscriber<>();
collection.deleteOne(eq("i", 110)).subscribe(deleteSubscriber);
deleteSubscriber.await();
// Delete Many
deleteSubscriber = new OperationSubscriber<>();
collection.deleteMany(gte("i", 100)).subscribe(deleteSubscriber);
deleteSubscriber.await();
// Create Index
OperationSubscriber<String> createIndexSubscriber = new PrintSubscriber<>("Create Index Result: %s");
collection.createIndex(new Document("i", 1)).subscribe(createIndexSubscriber);
createIndexSubscriber.await();
// Clean up
successSubscriber = new OperationSubscriber<>();
collection.drop().subscribe(successSubscriber);
successSubscriber.await();
// release resources
mongoClient.close();
}
Aggregations