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