Search in sources :

Example 86 with Document

use of org.bson.Document in project mongo-java-driver by mongodb.

the class CollectionAcceptanceTest method shouldBeAbleToRenameCollectionToAnExistingCollectionNameAndReplaceItWhenDropIsTrue.

@Test
public void shouldBeAbleToRenameCollectionToAnExistingCollectionNameAndReplaceItWhenDropIsTrue() {
    //given
    String existingCollectionName = "anExistingCollection";
    String keyInOriginalCollection = "someKey";
    String valueInOriginalCollection = "someValue";
    collection.insertOne(new Document(keyInOriginalCollection, valueInOriginalCollection));
    MongoCollection<Document> existingCollection = database.getCollection(existingCollectionName);
    String keyInExistingCollection = "aDifferentDocument";
    String valueInExistingCollection = "withADifferentValue";
    existingCollection.insertOne(new Document(keyInExistingCollection, valueInExistingCollection));
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(getCollectionName()), is(true));
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(existingCollectionName), is(true));
    //when
    collection.renameCollection(new MongoNamespace(getDatabaseName(), existingCollectionName), new RenameCollectionOptions().dropTarget(true));
    //then
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(getCollectionName()), is(false));
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(existingCollectionName), is(true));
    MongoCollection<Document> replacedCollection = database.getCollection(existingCollectionName);
    assertThat(replacedCollection.find().first().get(keyInExistingCollection), is(nullValue()));
    assertThat(replacedCollection.find().first().get(keyInOriginalCollection).toString(), is(valueInOriginalCollection));
}
Also used : RenameCollectionOptions(com.mongodb.client.model.RenameCollectionOptions) BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace) Test(org.junit.Test)

Example 87 with Document

use of org.bson.Document in project mongo-java-driver by mongodb.

the class CollectionAcceptanceTest method shouldSortDocumentsWhenUsingAggregate.

@Test
public void shouldSortDocumentsWhenUsingAggregate() {
    List<Document> documents = insertAggregationTestDocuments();
    List<Document> sorted = collection.aggregate(asList(new Document("$sort", new Document("_id", 1)))).into(new ArrayList<Document>());
    assertEquals(documents, sorted);
}
Also used : Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 88 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 89 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 90 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)

Aggregations

Document (org.bson.Document)1104 Test (org.junit.Test)795 ArrayList (java.util.ArrayList)74 Update (org.springframework.data.mongodb.core.query.Update)71 List (java.util.List)55 BsonDocument (org.bson.BsonDocument)53 ObjectId (org.bson.types.ObjectId)41 MongoDatabase (com.mongodb.client.MongoDatabase)40 Query (org.springframework.data.mongodb.core.query.Query)40 BasicDBObject (com.mongodb.BasicDBObject)39 MongoClient (com.mongodb.MongoClient)32 Bson (org.bson.conversions.Bson)32 ReturnDocument (com.mongodb.client.model.ReturnDocument)31 DBObject (com.mongodb.DBObject)27 DBRef (com.mongodb.DBRef)25 UnknownHostException (java.net.UnknownHostException)25 HashMap (java.util.HashMap)24 FullDocument (com.mongodb.client.model.changestream.FullDocument)23 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)21 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)21