Search in sources :

Example 1 with ReturnDocument

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));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) ObjectId(org.bson.types.ObjectId) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) WorkerCodecProvider(com.mongodb.client.test.WorkerCodecProvider) Worker(com.mongodb.client.test.Worker) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) 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 ReturnDocument

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));
}
Also used : FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) Test(org.junit.Test)

Example 3 with ReturnDocument

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));
}
Also used : FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) Test(org.junit.Test)

Example 4 with ReturnDocument

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));
}
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 5 with ReturnDocument

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

Aggregations

ReturnDocument (com.mongodb.client.model.ReturnDocument)6 Document (org.bson.Document)6 Test (org.junit.Test)6 ObjectId (org.bson.types.ObjectId)4 FindOneAndReplaceOptions (com.mongodb.client.model.FindOneAndReplaceOptions)3 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)3 Worker (com.mongodb.client.test.Worker)2 WorkerCodecProvider (com.mongodb.client.test.WorkerCodecProvider)2 Date (java.util.Date)2 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)2 DocumentCodecProvider (org.bson.codecs.DocumentCodecProvider)2 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)2 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)2