use of org.bson.Document in project mongo-java-driver by mongodb.
the class FindAndDeleteAcceptanceTest method shouldRemoveOnlyOneDocumentWhenMultipleDocumentsMatchSearch.
@Test
public void shouldRemoveOnlyOneDocumentWhenMultipleDocumentsMatchSearch() {
// given
Document documentInserted = new Document(KEY, VALUE_TO_CARE_ABOUT);
collection.insertOne(documentInserted);
collection.insertOne(new Document(KEY, VALUE_TO_CARE_ABOUT));
collection.insertOne(new Document(KEY, VALUE_TO_CARE_ABOUT));
assertThat(collection.count(), is(3L));
// when
Document filter = new Document(KEY, VALUE_TO_CARE_ABOUT);
assertThat(collection.count(filter, new CountOptions()), is(3L));
Document documentRetrieved = collection.findOneAndDelete(filter);
// then
assertThat("Document should have been deleted from the collection", collection.count(), is(2L));
assertThat("Document retrieved from removeAndGet should match the document that was inserted", documentRetrieved, equalTo(documentInserted));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class FindAndDeleteAcceptanceTest method shouldRemoveOnlyTheFirstValueMatchedByFilter.
@Test
public void shouldRemoveOnlyTheFirstValueMatchedByFilter() {
// given
String secondKey = "secondKey";
Document documentToRemove = new Document(KEY, VALUE_TO_CARE_ABOUT).append(secondKey, 1);
//inserting in non-ordered fashion
collection.insertOne(new Document(KEY, VALUE_TO_CARE_ABOUT).append(secondKey, 2));
collection.insertOne(new Document(KEY, VALUE_TO_CARE_ABOUT).append(secondKey, 3));
collection.insertOne(documentToRemove);
assertThat(collection.count(), is(3L));
// when
Document filter = new Document(KEY, VALUE_TO_CARE_ABOUT);
Document documentRetrieved = collection.findOneAndDelete(filter, new FindOneAndDeleteOptions().sort(new Document(secondKey, 1)));
// then
assertThat("Document should have been deleted from the collection", collection.count(), is(2L));
assertThat("Document retrieved from removeAndGet should match the document that was inserted", documentRetrieved, equalTo(documentToRemove));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class FindAndReplaceAcceptanceTest method shouldReplaceAndReturnOriginalItemWithDocumentRequiringACustomEncoder.
@Test
public void shouldReplaceAndReturnOriginalItemWithDocumentRequiringACustomEncoder() {
Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 0);
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(), 1);
Worker returnedDocument = collection.findOneAndReplace(new Document("name", "Pat"), jordan);
assertThat("Document, retrieved from getOneAndReplace, should match the document inserted before", returnedDocument, equalTo(pat));
}
use of org.bson.Document 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 org.bson.Document 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