Search in sources :

Example 26 with Document

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

Example 27 with Document

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

Example 28 with Document

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));
}
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) 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 29 with Document

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));
}
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 30 with Document

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

Document (org.bson.Document)2386 Test (org.junit.jupiter.api.Test)900 Test (org.junit.Test)472 ArrayList (java.util.ArrayList)209 BsonDocument (org.bson.BsonDocument)188 List (java.util.List)160 Bson (org.bson.conversions.Bson)133 MongoDatabase (com.mongodb.client.MongoDatabase)119 Update (org.springframework.data.mongodb.core.query.Update)116 ObjectId (org.bson.types.ObjectId)113 Map (java.util.Map)92 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)83 Test (org.testng.annotations.Test)82 HashMap (java.util.HashMap)79 BasicDBObject (com.mongodb.BasicDBObject)75 Query (org.springframework.data.mongodb.core.query.Query)67 MongoCollection (com.mongodb.client.MongoCollection)54 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)50 MongoClient (com.mongodb.MongoClient)48 Date (java.util.Date)47