Search in sources :

Example 41 with Document

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

Example 42 with Document

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

Example 43 with Document

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

Example 44 with Document

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

Example 45 with Document

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