Search in sources :

Example 1 with FindOneAndReplaceOptions

use of com.mongodb.client.model.FindOneAndReplaceOptions in project mongo-java-driver by mongodb.

the class FindAndReplaceAcceptanceTest method shouldReplaceAndReturnNewItemWithDocumentRequiringACustomEncoder.

@Test
public void shouldReplaceAndReturnNewItemWithDocumentRequiringACustomEncoder() {
    Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 3);
    CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new WorkerCodecProvider()));
    MongoCollection<Worker> collection = database.getCollection(getCollectionName(), Worker.class).withCodecRegistry(codecRegistry);
    collection.insertOne(pat);
    assertThat(collection.count(), is(1L));
    Worker jordan = new Worker(pat.getId(), "Jordan", "Engineer", new Date(), 7);
    Worker returnedDocument = collection.findOneAndReplace(new Document("name", "Pat"), jordan, new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER));
    assertThat("Worker retrieved from replaceOneAndGet should match the updated Worker", returnedDocument, equalTo(jordan));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) ObjectId(org.bson.types.ObjectId) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) WorkerCodecProvider(com.mongodb.client.test.WorkerCodecProvider) Worker(com.mongodb.client.test.Worker) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Date(java.util.Date) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 2 with FindOneAndReplaceOptions

use of com.mongodb.client.model.FindOneAndReplaceOptions in project mongo-java-driver by mongodb.

the class FindAndReplaceAcceptanceTest method shouldInsertDocumentWhenFilterDoesNotMatchAnyDocumentsAndUpsertFlagIsSet.

@Test
public void shouldInsertDocumentWhenFilterDoesNotMatchAnyDocumentsAndUpsertFlagIsSet() {
    Document originalDocument = new Document(KEY, VALUE_TO_CARE_ABOUT);
    collection.insertOne(originalDocument);
    assertThat(collection.count(), is(1L));
    Document replacementDocument = new Document("_id", new ObjectId()).append("foo", "bar");
    Document document = collection.findOneAndReplace(new Document(KEY, "valueThatDoesNotMatch"), replacementDocument, new FindOneAndReplaceOptions().upsert(true).returnDocument(ReturnDocument.AFTER));
    assertThat(collection.count(), is(2L));
    assertThat("Document retrieved from replaceOneAndGet with filter that doesn't match should match the replacement document", document, equalTo(replacementDocument));
}
Also used : FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) Test(org.junit.Test)

Example 3 with FindOneAndReplaceOptions

use of com.mongodb.client.model.FindOneAndReplaceOptions in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeFindOneAndReplace.

OperationResult executeFindOneAndReplace(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    BsonDocument filter = arguments.getDocument("filter").asDocument();
    BsonDocument replacement = arguments.getDocument("replacement").asDocument();
    FindOneAndReplaceOptions options = new FindOneAndReplaceOptions();
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "filter":
            case "replacement":
                break;
            case "returnDocument":
                switch(cur.getValue().asString().getValue()) {
                    case "Before":
                        options.returnDocument(ReturnDocument.BEFORE);
                        break;
                    case "After":
                        options.returnDocument(ReturnDocument.AFTER);
                        break;
                    default:
                        throw new UnsupportedOperationException("Can't happen");
                }
                break;
            case "hint":
                if (cur.getValue().isString()) {
                    options.hintString(cur.getValue().asString().getValue());
                } else {
                    options.hint(cur.getValue().asDocument());
                }
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> collection.findOneAndReplace(filter, replacement, options));
}
Also used : FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 4 with FindOneAndReplaceOptions

use of com.mongodb.client.model.FindOneAndReplaceOptions in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUnitTests method findAndReplaceShouldUseCollationEvenIfDefaultCollationIsPresent.

// DATAMONGO-1854
@Test
void findAndReplaceShouldUseCollationEvenIfDefaultCollationIsPresent() {
    template.findAndReplace(new BasicQuery("{}").collation(Collation.of("fr")), new MongoTemplateUnitTests.Sith()).subscribe();
    ArgumentCaptor<FindOneAndReplaceOptions> options = ArgumentCaptor.forClass(FindOneAndReplaceOptions.class);
    verify(collection).findOneAndReplace(any(Bson.class), any(), options.capture());
    assertThat(options.getValue().getCollation().getLocale()).isEqualTo("fr");
}
Also used : FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 5 with FindOneAndReplaceOptions

use of com.mongodb.client.model.FindOneAndReplaceOptions in project mongo-java-driver by mongodb.

the class JsonPoweredCrudTestHelper method getFindOneAndReplaceResult.

BsonDocument getFindOneAndReplaceResult(final BsonDocument arguments) {
    // in 2.4 the server can ignore the supplied _id and creates an ObjectID
    Assume.assumeTrue(serverVersionAtLeast(2, 6));
    FindOneAndReplaceOptions options = new FindOneAndReplaceOptions();
    if (arguments.containsKey("projection")) {
        options.projection(arguments.getDocument("projection"));
    }
    if (arguments.containsKey("sort")) {
        options.sort(arguments.getDocument("sort"));
    }
    if (arguments.containsKey("upsert")) {
        options.upsert(arguments.getBoolean("upsert").getValue());
    }
    if (arguments.containsKey("returnDocument")) {
        options.returnDocument(arguments.getString("returnDocument").getValue().equals("After") ? ReturnDocument.AFTER : ReturnDocument.BEFORE);
    }
    if (arguments.containsKey("collation")) {
        options.collation(getCollation(arguments.getDocument("collation")));
    }
    return toResult(collection.findOneAndReplace(arguments.getDocument("filter"), arguments.getDocument("replacement"), options));
}
Also used : FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions)

Aggregations

FindOneAndReplaceOptions (com.mongodb.client.model.FindOneAndReplaceOptions)13 Document (org.bson.Document)5 ReturnDocument (com.mongodb.client.model.ReturnDocument)4 Test (org.junit.jupiter.api.Test)4 BsonDocument (org.bson.BsonDocument)3 ObjectId (org.bson.types.ObjectId)3 Test (org.junit.Test)3 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)3 StorageException (io.lumeer.storage.api.exception.StorageException)2 Bson (org.bson.conversions.Bson)2 MongoException (com.mongodb.MongoException)1 Worker (com.mongodb.client.test.Worker)1 WorkerCodecProvider (com.mongodb.client.test.WorkerCodecProvider)1 JsonView (io.lumeer.api.dto.JsonView)1 View (io.lumeer.api.model.View)1 DataDocument (io.lumeer.engine.api.data.DataDocument)1 Date (java.util.Date)1 Map (java.util.Map)1 BsonString (org.bson.BsonString)1 BsonValue (org.bson.BsonValue)1