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));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class DropIndexAcceptanceTest method shouldDropSingleNamedIndex.
@Test
public void shouldDropSingleNamedIndex() {
// Given
collection.createIndex(new Document("field", 1));
assertThat("Should be default index and new index on the database now", collection.listIndexes().into(new ArrayList<Document>()).size(), is(2));
// When
collection.dropIndex("field_1");
// Then
assertThat("Should be one less index", collection.listIndexes().into(new ArrayList<Document>()).size(), is(1));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class FilterAcceptanceTest method shouldReturnResultsInTheReverseOrderTheyAreOnDiskWhenNaturalSortOfMinusOneApplied.
@Test
public void shouldReturnResultsInTheReverseOrderTheyAreOnDiskWhenNaturalSortOfMinusOneApplied() {
// Given
collection.insertOne(new Document("name", "Chris"));
collection.insertOne(new Document("name", "Adam"));
collection.insertOne(new Document("name", "Bob"));
// When
MongoCursor<Document> sortedCollection = collection.find().sort(new Document("$natural", -1)).iterator();
// Then
assertThat(sortedCollection.next().get("name").toString(), is("Bob"));
assertThat(sortedCollection.next().get("name").toString(), is("Adam"));
assertThat(sortedCollection.next().get("name").toString(), is("Chris"));
}
Aggregations