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));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class AddIndexAcceptanceTest method shouldCreateASparseIndex.
@Test
public void shouldCreateASparseIndex() {
collection.createIndex(new Document("theField", 1), new IndexOptions().sparse(true));
Boolean sparse = collection.listIndexes().into(new ArrayList<Document>()).get(1).getBoolean("sparse");
assertThat("Should be a sparse index", sparse, is(true));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class AddIndexAcceptanceTest method shouldSupportCompoundIndexesWithDifferentOrders.
@Test
public void shouldSupportCompoundIndexesWithDifferentOrders() {
collection.createIndex(new Document("theFirstField", 1).append("theSecondField", -1));
Document newIndexDetails = collection.listIndexes().into(new ArrayList<Document>()).get(1);
Document keys = (Document) newIndexDetails.get("key");
OrderBy orderBy = fromInt((Integer) keys.get("theFirstField"));
assertThat("First index should be ascending", orderBy, is(ASC));
orderBy = fromInt((Integer) keys.get("theSecondField"));
assertThat("Second index should be descending", orderBy, is(DESC));
assertThat("Index name should contain both field names", (String) newIndexDetails.get("name"), is("theFirstField_1_theSecondField_-1"));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class DropIndexAcceptanceTest method shouldDropAllIndexesForCollection.
@Test
public void shouldDropAllIndexesForCollection() {
// Given
collection.createIndex(new Document("field", 1));
collection.createIndex(new Document("anotherField", 1));
assertThat("Should be three indexes on the collection now", collection.listIndexes().into(new ArrayList<Document>()).size(), is(3));
// When
collection.dropIndexes();
// Then
assertThat("Should only be the default index on the collection", collection.listIndexes().into(new ArrayList<Document>()).size(), is(1));
}
Aggregations