use of com.mongodb.client.model.FindOneAndReplaceOptions in project mongo-java-driver by mongodb.
the class CrudTest method getFindOneAndReplaceMongoOperation.
private MongoOperationBsonDocument getFindOneAndReplaceMongoOperation(final BsonDocument arguments) {
return new MongoOperationBsonDocument() {
@Override
public void execute() {
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")));
}
collection.findOneAndReplace(arguments.getDocument("filter"), arguments.getDocument("replacement"), options, getCallback());
}
};
}
use of com.mongodb.client.model.FindOneAndReplaceOptions in project mongo-java-driver by mongodb.
the class FindAndReplaceAcceptanceTest method shouldReturnNewDocumentAfterReplaceWhenUsingReplaceOneAndGet.
@Test
public void shouldReturnNewDocumentAfterReplaceWhenUsingReplaceOneAndGet() {
ObjectId id = new ObjectId();
Document documentInserted = new Document("_id", id).append(KEY, VALUE_TO_CARE_ABOUT);
Document documentReplacement = new Document("_id", id).append("foo", "bar");
collection.insertOne(documentInserted);
assertThat(collection.count(), is(1L));
Document document = collection.findOneAndReplace(new Document(KEY, VALUE_TO_CARE_ABOUT), documentReplacement, new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER));
assertThat("Document, retrieved from replaceAndGet after change applied should match the document used as replacement", document, equalTo(documentReplacement));
}
use of com.mongodb.client.model.FindOneAndReplaceOptions in project mongo-java-driver by mongodb.
the class MongoCollectionImplTest method testFindOneAndReplace.
@Test
public void testFindOneAndReplace() {
FindOneAndReplaceOptions options = new FindOneAndReplaceOptions().collation(collation);
Document replacement = new Document();
assertAll("findOneAndReplace", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndReplace(null, replacement)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndReplace(filter, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndReplace(clientSession, null, replacement)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndReplace(clientSession, filter, replacement, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndReplace(null, filter, replacement)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndReplace(null, filter, replacement, options))), () -> {
Publisher<Document> expected = mongoOperationPublisher.findOneAndReplace(null, filter, replacement, new FindOneAndReplaceOptions());
assertPublisherIsTheSameAs(expected, collection.findOneAndReplace(filter, replacement), "Default");
}, () -> {
Publisher<Document> expected = mongoOperationPublisher.findOneAndReplace(null, filter, replacement, options);
assertPublisherIsTheSameAs(expected, collection.findOneAndReplace(filter, replacement, options), "With filter & options");
}, () -> {
Publisher<Document> expected = mongoOperationPublisher.findOneAndReplace(clientSession, filter, replacement, new FindOneAndReplaceOptions());
assertPublisherIsTheSameAs(expected, collection.findOneAndReplace(clientSession, filter, replacement), "With client session");
}, () -> {
Publisher<Document> expected = mongoOperationPublisher.findOneAndReplace(clientSession, filter, replacement, options);
assertPublisherIsTheSameAs(expected, collection.findOneAndReplace(clientSession, filter, replacement, options), "With client session, filter & options");
});
}
use of com.mongodb.client.model.FindOneAndReplaceOptions in project mongo-java-driver by mongodb.
the class JsonPoweredCrudTestHelper method getFindOneAndReplaceResult.
BsonDocument getFindOneAndReplaceResult(final BsonDocument collectionOptions, final BsonDocument arguments, @Nullable final ClientSession clientSession) {
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")));
}
if (arguments.containsKey("hint")) {
if (arguments.isDocument("hint")) {
options.hint(arguments.getDocument("hint"));
} else {
options.hintString(arguments.getString("hint").getValue());
}
}
BsonDocument result;
if (clientSession == null) {
result = getCollection(collectionOptions).findOneAndReplace(arguments.getDocument("filter"), arguments.getDocument("replacement"), options);
} else {
result = getCollection(collectionOptions).findOneAndReplace(clientSession, arguments.getDocument("filter"), arguments.getDocument("replacement"), options);
}
return toResult(result);
}
use of com.mongodb.client.model.FindOneAndReplaceOptions in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateUnitTests method findAndReplaceShouldUseCollationWhenPresent.
// DATAMONGO-1854
@Test
void findAndReplaceShouldUseCollationWhenPresent() {
template.findAndReplace(new BasicQuery("{}").collation(Collation.of("fr")), new Jedi()).subscribe();
ArgumentCaptor<FindOneAndReplaceOptions> options = ArgumentCaptor.forClass(FindOneAndReplaceOptions.class);
verify(collection).findOneAndReplace(any(Bson.class), any(), options.capture());
assertThat(options.getValue().getCollation().getLocale()).isEqualTo("fr");
}
Aggregations