Search in sources :

Example 1 with PrintSubscriber

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

the class UpdatePrimer method updateTopLevelFields.

@Test
public void updateTopLevelFields() {
    // @begin: update-top-level-fields
    ObservableSubscriber<UpdateResult> updateSubscriber = new PrintSubscriber<>("Update complete: %s");
    db.getCollection("restaurants").updateOne(new Document("name", "Juni"), new Document("$set", new Document("cuisine", "American (New)")).append("$currentDate", new Document("lastModified", true))).subscribe(updateSubscriber);
    updateSubscriber.await();
/*
        // @post: start
            The updateOne operation returns a ``UpdateResult`` which contains information about the operation.
            The ``getModifiedCount`` method returns the number of documents modified
        // @post: end
        */
// @end: update-top-level-fields
}
Also used : PrintSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintSubscriber) Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.junit.Test)

Example 2 with PrintSubscriber

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

the class UpdatePrimer method updateEmbeddedField.

@Test
public void updateEmbeddedField() {
    // @begin: update-top-level-fields
    ObservableSubscriber<UpdateResult> updateSubscriber = new PrintSubscriber<>("Update complete: %s");
    db.getCollection("restaurants").updateOne(new Document("restaurant_id", "41156888"), new Document("$set", new Document("address.street", "East 31st Street"))).subscribe(updateSubscriber);
    updateSubscriber.await();
/*
        // @post: start
            The updateOne operation returns a ``UpdateResult`` which contains information about the operation.
            The ``getModifiedCount`` method returns the number of documents modified
        // @post: end
        */
// @end: update-top-level-fields
}
Also used : PrintSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintSubscriber) Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.junit.Test)

Example 3 with PrintSubscriber

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

the class UpdatePrimer method updateMultipleDocuments.

@Test
public void updateMultipleDocuments() {
    // @begin: update-multiple-documents
    ObservableSubscriber<UpdateResult> updateSubscriber = new PrintSubscriber<>("Update complete: %s");
    db.getCollection("restaurants").updateMany(new Document("address.zipcode", "10016").append("cuisine", "Other"), new Document("$set", new Document("cuisine", "Category To Be Determined")).append("$currentDate", new Document("lastModified", true))).subscribe(updateSubscriber);
    updateSubscriber.await();
/*
        // @post: start
            The updateMany operation returns a ``UpdateResult`` which contains information about the operation.
            The ``getModifiedCount`` method returns the number of documents modified
        // @post: end
        */
// @end: update-multiple-documents
}
Also used : PrintSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintSubscriber) Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.junit.Test)

Example 4 with PrintSubscriber

use of reactivestreams.helpers.SubscriberHelpers.PrintSubscriber 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();
}
Also used : PrintDocumentSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber) OperationSubscriber(reactivestreams.helpers.SubscriberHelpers.OperationSubscriber) ArrayList(java.util.ArrayList) Document(org.bson.Document) InsertManyResult(com.mongodb.client.result.InsertManyResult) MongoClient(com.mongodb.reactivestreams.client.MongoClient) PrintSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintSubscriber) InsertOneResult(com.mongodb.client.result.InsertOneResult) UpdateResult(com.mongodb.client.result.UpdateResult) DeleteResult(com.mongodb.client.result.DeleteResult) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase)

Example 5 with PrintSubscriber

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

the class IndexesPrimer method createCompoundIndex.

@Test
public void createCompoundIndex() {
    // @begin: create-compound-index
    // @code: start
    ObservableSubscriber<String> indexSubscriber = new PrintSubscriber<>("Index created: %s");
    db.getCollection("restaurants").createIndex(new Document("cuisine", 1).append("address.zipcode", 1)).subscribe(indexSubscriber);
    indexSubscriber.await();
// @code: end
// @post: The method does not return a result.
// @end: create-compound-index
}
Also used : PrintSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintSubscriber) Document(org.bson.Document) Test(org.junit.Test)

Aggregations

Document (org.bson.Document)5 PrintSubscriber (reactivestreams.helpers.SubscriberHelpers.PrintSubscriber)5 UpdateResult (com.mongodb.client.result.UpdateResult)4 Test (org.junit.Test)4 DeleteResult (com.mongodb.client.result.DeleteResult)1 InsertManyResult (com.mongodb.client.result.InsertManyResult)1 InsertOneResult (com.mongodb.client.result.InsertOneResult)1 MongoClient (com.mongodb.reactivestreams.client.MongoClient)1 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)1 ArrayList (java.util.ArrayList)1 OperationSubscriber (reactivestreams.helpers.SubscriberHelpers.OperationSubscriber)1 PrintDocumentSubscriber (reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber)1