use of org.bson.Document in project mongo-java-driver by mongodb.
the class ReplaceAcceptanceTest method shouldReplaceTheDocumentIfReplacingWithUpsertAndDocumentIsFoundInCollection.
@Test
public void shouldReplaceTheDocumentIfReplacingWithUpsertAndDocumentIsFoundInCollection() {
// given
Document originalDocument = new Document("_id", 3).append("x", 2);
collection.replaceOne(new Document(), originalDocument, new UpdateOptions().upsert(true));
assertThat(collection.count(), is(1L));
// when
Document replacement = originalDocument.append("y", 5);
collection.replaceOne(new Document(), replacement, new UpdateOptions().upsert(true));
// then
assertThat(collection.count(), is(1L));
assertThat(collection.find(new Document("_id", 3)).iterator().next(), is(replacement));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class ReplaceAcceptanceTest method shouldThrowExceptionIfTryingToChangeTheIdOfADocument.
@Test
public void shouldThrowExceptionIfTryingToChangeTheIdOfADocument() {
// Given
Document firstDocument = new Document("_id", 1).append("a", 1);
collection.insertOne(firstDocument);
// When
Document filter = new Document("a", 1);
Document newDocumentWithDifferentId = new Document("_id", 2).append("a", 3);
try {
collection.replaceOne(filter, newDocumentWithDifferentId);
fail("Should have thrown an exception");
} catch (MongoWriteException e) {
// Then
assertThat("Error code should match one of these error codes", e.getCode(), anyOf(is(13596), is(16837)));
}
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class UpdateAcceptanceTest method shouldInsertTheDocumentIfUpdatingOneWithUpsertAndDocumentNotFoundInCollection.
@Test
public void shouldInsertTheDocumentIfUpdatingOneWithUpsertAndDocumentNotFoundInCollection() {
// given
Document originalDocument = new Document("_id", 1).append("x", 2);
collection.insertOne(originalDocument);
// when
Document filter = new Document("_id", 2);
collection.updateOne(filter, new Document("$set", new Document("x", 5)), new UpdateOptions().upsert(true));
// then
assertThat(collection.count(), is(2L));
Document expectedDocument = new Document("_id", 2).append("x", 5);
assertThat(collection.find(filter).iterator().next(), is(expectedDocument));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class UpdateAcceptanceTest method shouldSetANewFieldOnAnExistingDocument.
@Test
public void shouldSetANewFieldOnAnExistingDocument() {
// given
Document originalDocument = new Document("_id", 1);
collection.insertOne(originalDocument);
// when
collection.updateOne(new Document("_id", 1), new Document("$set", new Document("x", 2)));
// then
assertThat(collection.count(), is(1L));
Document expectedDocument = new Document("_id", 1).append("x", 2);
assertThat(collection.find().first(), is(expectedDocument));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class UpdateAcceptanceTest method shouldUpdateAllDocumentsIfUpdatingAllWithUpsertAndMultipleDocumentsFoundInCollection.
@Test
public void shouldUpdateAllDocumentsIfUpdatingAllWithUpsertAndMultipleDocumentsFoundInCollection() {
// given
Document firstDocument = new Document("_id", 1).append("x", 3);
collection.insertOne(firstDocument);
Document secondDocument = new Document("_id", 2).append("x", 3);
collection.insertOne(secondDocument);
// when
Document filter = new Document("x", 3);
collection.updateMany(filter, new Document("$set", new Document("x", 5)), new UpdateOptions().upsert(true));
// then
assertThat(collection.count(new Document("x", 5), new CountOptions()), is(2L));
}
Aggregations