Search in sources :

Example 71 with UpdateResult

use of com.mongodb.client.result.UpdateResult in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeUpdateOne.

OperationResult executeUpdateOne(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    ClientSession session = getSession(arguments);
    BsonDocument filter = arguments.getDocument("filter");
    BsonValue update = arguments.get("update");
    UpdateOptions options = getUpdateOptions(arguments);
    return resultOf(() -> {
        UpdateResult updateResult;
        if (session == null) {
            updateResult = update.isArray() ? collection.updateOne(filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options) : collection.updateOne(filter, update.asDocument(), options);
        } else {
            updateResult = update.isArray() ? collection.updateOne(session, filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options) : collection.updateOne(session, filter, update.asDocument(), options);
        }
        return toExpected(updateResult);
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) UpdateResult(com.mongodb.client.result.UpdateResult) BsonValue(org.bson.BsonValue)

Example 72 with UpdateResult

use of com.mongodb.client.result.UpdateResult 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 73 with UpdateResult

use of com.mongodb.client.result.UpdateResult in project mongo-java-driver by mongodb.

the class JsonPoweredCrudTestHelper method getReplaceOneResult.

BsonDocument getReplaceOneResult(final BsonDocument collectionOptions, final BsonDocument arguments, @Nullable final ClientSession clientSession) {
    ReplaceOptions options = new ReplaceOptions();
    if (arguments.containsKey("upsert")) {
        options.upsert(arguments.getBoolean("upsert").getValue());
    }
    if (arguments.containsKey("collation")) {
        options.collation(getCollation(arguments.getDocument("collation")));
    }
    if (arguments.containsKey("bypassDocumentValidation")) {
        options.bypassDocumentValidation(arguments.getBoolean("bypassDocumentValidation").getValue());
    }
    if (arguments.containsKey("hint")) {
        if (arguments.isDocument("hint")) {
            options.hint(arguments.getDocument("hint"));
        } else {
            options.hintString(arguments.getString("hint").getValue());
        }
    }
    UpdateResult updateResult;
    if (clientSession == null) {
        updateResult = getCollection(collectionOptions).replaceOne(arguments.getDocument("filter"), arguments.getDocument("replacement"), options);
    } else {
        updateResult = getCollection(collectionOptions).replaceOne(clientSession, arguments.getDocument("filter"), arguments.getDocument("replacement"), options);
    }
    return toResult(updateResult);
}
Also used : ReplaceOptions(com.mongodb.client.model.ReplaceOptions) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 74 with UpdateResult

use of com.mongodb.client.result.UpdateResult in project mongo-java-driver by mongodb.

the class JsonPoweredCrudTestHelper method getUpdateOneResult.

@SuppressWarnings("unchecked")
BsonDocument getUpdateOneResult(final BsonDocument collectionOptions, final BsonDocument arguments, @Nullable final ClientSession clientSession) {
    UpdateOptions options = new UpdateOptions();
    if (arguments.containsKey("upsert")) {
        options.upsert(arguments.getBoolean("upsert").getValue());
    }
    if (arguments.containsKey("collation")) {
        options.collation(getCollation(arguments.getDocument("collation")));
    }
    if (arguments.containsKey("arrayFilters")) {
        options.arrayFilters((getListOfDocuments(arguments.getArray("arrayFilters"))));
    }
    if (arguments.containsKey("bypassDocumentValidation")) {
        options.bypassDocumentValidation(arguments.getBoolean("bypassDocumentValidation").getValue());
    }
    if (arguments.containsKey("hint")) {
        if (arguments.isDocument("hint")) {
            options.hint(arguments.getDocument("hint"));
        } else {
            options.hintString(arguments.getString("hint").getValue());
        }
    }
    UpdateResult updateResult;
    if (clientSession == null) {
        if (arguments.isDocument("update")) {
            updateResult = getCollection(collectionOptions).updateOne(arguments.getDocument("filter"), arguments.getDocument("update"), options);
        } else {
            // update is a pipeline
            updateResult = getCollection(collectionOptions).updateOne(arguments.getDocument("filter"), getListOfDocuments(arguments.getArray("update")), options);
        }
    } else {
        if (arguments.isDocument("update")) {
            updateResult = getCollection(collectionOptions).updateOne(clientSession, arguments.getDocument("filter"), arguments.getDocument("update"), options);
        } else {
            // update is a pipeline
            updateResult = getCollection(collectionOptions).updateOne(clientSession, arguments.getDocument("filter"), getListOfDocuments(arguments.getArray("update")), options);
        }
    }
    return toResult(updateResult);
}
Also used : UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) UpdateResult(com.mongodb.client.result.UpdateResult)

Aggregations

UpdateResult (com.mongodb.client.result.UpdateResult)74 Document (org.bson.Document)31 Test (org.junit.Test)20 Bson (org.bson.conversions.Bson)16 Update (org.springframework.data.mongodb.core.query.Update)12 Test (org.testng.annotations.Test)11 UpdateOptions (com.mongodb.client.model.UpdateOptions)10 BasicDBObject (com.mongodb.BasicDBObject)9 FindOptions (dev.morphia.query.FindOptions)8 BsonDocument (org.bson.BsonDocument)8 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)7 DeleteResult (com.mongodb.client.result.DeleteResult)7 Query (org.springframework.data.mongodb.core.query.Query)7 ObjectId (org.bson.types.ObjectId)6 UpdateOptions (dev.morphia.UpdateOptions)5 ArrayList (java.util.ArrayList)5 Exchange (org.apache.camel.Exchange)5 Processor (org.apache.camel.Processor)5 DBObject (com.mongodb.DBObject)4 Formatter (java.util.Formatter)4