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));
}
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));
}
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));
}
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");
}
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));
}
Aggregations