use of com.mongodb.client.model.ReturnDocument in project mongo-java-driver by mongodb.
the class FindAndUpdateAcceptanceTest method shouldFindAndReplaceWithDocumentRequiringACustomEncoder.
@Test
public void shouldFindAndReplaceWithDocumentRequiringACustomEncoder() {
Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 7);
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));
Document updateOperation = new Document("$inc", new Document("numberOfJobs", 1));
Worker updatedDocument = collection.findOneAndUpdate(new Document("name", "Pat"), updateOperation, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
assertThat("Worker returned from updateOneAndGet should have the", updatedDocument.getNumberOfJobs(), equalTo(8));
}
use of com.mongodb.client.model.ReturnDocument in project mongo-java-driver by mongodb.
the class FindAndUpdateAcceptanceTest method shouldUpdateDocumentAndReturnNew.
@Test
public void shouldUpdateDocumentAndReturnNew() {
Document documentInserted = new Document(KEY, VALUE_TO_CARE_ABOUT).append("someNumber", 11);
collection.insertOne(documentInserted);
assertThat(collection.count(), is(1L));
Document updateOperation = new Document("$inc", new Document("someNumber", 1));
Document updatedDocument = collection.findOneAndUpdate(new Document(KEY, VALUE_TO_CARE_ABOUT), updateOperation, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
assertThat("Document returned from updateOneAndGet should be the updated document", (Integer) updatedDocument.get("someNumber"), equalTo(12));
}
use of com.mongodb.client.model.ReturnDocument in project mongo-java-driver by mongodb.
the class FindAndUpdateAcceptanceTest method shouldInsertDocumentWhenFilterDoesNotMatchAnyDocumentsAndUpsertSelected.
@Test
public void shouldInsertDocumentWhenFilterDoesNotMatchAnyDocumentsAndUpsertSelected() {
Document originalDocument = new Document(KEY, VALUE_TO_CARE_ABOUT).append("someNumber", 11);
collection.insertOne(originalDocument);
assertThat(collection.count(), is(1L));
String newValueThatDoesNotMatchAnythingInDatabase = "valueThatDoesNotMatch";
Document updateOperation = new Document("$inc", new Document("someNumber", 1));
Document document = collection.findOneAndUpdate(new Document(KEY, newValueThatDoesNotMatchAnythingInDatabase), updateOperation, new FindOneAndUpdateOptions().upsert(true).returnDocument(ReturnDocument.AFTER));
assertThat(collection.count(), is(2L));
assertThat("Document retrieved from updateOneAndGet and upsert true should have the new values", (Integer) document.get("someNumber"), equalTo(1));
assertThat("Document retrieved from updateOneAndGet and upsert true should have the new values", document.get(KEY).toString(), equalTo(newValueThatDoesNotMatchAnythingInDatabase));
}
use of com.mongodb.client.model.ReturnDocument 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.ReturnDocument 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));
}
Aggregations