use of com.mongodb.client.model.IndexOptions in project mongo-java-driver by mongodb.
the class GridFSBucketImpl method checkCreateIndex.
private void checkCreateIndex() {
if (!checkedIndexes) {
if (filesCollection.withDocumentClass(Document.class).withReadPreference(primary()).find().projection(new Document("_id", 1)).first() == null) {
Document filesIndex = new Document("filename", 1).append("uploadDate", 1);
if (!hasIndex(filesCollection.withReadPreference(primary()), filesIndex)) {
filesCollection.createIndex(filesIndex);
}
Document chunksIndex = new Document("files_id", 1).append("n", 1);
if (!hasIndex(chunksCollection.withReadPreference(primary()), chunksIndex)) {
chunksCollection.createIndex(chunksIndex, new IndexOptions().unique(true));
}
}
checkedIndexes = true;
}
}
use of com.mongodb.client.model.IndexOptions 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 com.mongodb.client.model.IndexOptions in project mongo-java-driver by mongodb.
the class AddIndexAcceptanceTest method shouldCreateATtlIndex.
@Test
public void shouldCreateATtlIndex() {
collection.createIndex(new Document("theField", 1), new IndexOptions().expireAfter(1600L, TimeUnit.SECONDS));
Long ttl = collection.listIndexes().into(new ArrayList<Document>()).get(1).getLong("expireAfterSeconds");
assertThat("Should be a ttl index", ttl, is(1600L));
}
use of com.mongodb.client.model.IndexOptions in project mongo-java-driver by mongodb.
the class AddIndexAcceptanceTest method shouldAllowAliasForIndex.
@Test
public void shouldAllowAliasForIndex() {
String indexAlias = "indexAlias";
collection.createIndex(new Document("theField", 1), new IndexOptions().name(indexAlias));
String nameOfCreatedIndex = collection.listIndexes().into(new ArrayList<Document>()).get(1).getString("name");
assertThat("Should be an index named after the alias", nameOfCreatedIndex, is(indexAlias));
}
use of com.mongodb.client.model.IndexOptions in project mongo-java-driver by mongodb.
the class AddIndexAcceptanceTest method shouldCreateIndexOfUniqueValues.
@Test
public void shouldCreateIndexOfUniqueValues() {
collection.createIndex(new Document("field", 1), new IndexOptions().unique(true));
Document newIndexDetails = collection.listIndexes().into(new ArrayList<Document>()).get(1);
Boolean unique = (Boolean) newIndexDetails.get("unique");
assertThat("Index created should be unique", unique, is(true));
}
Aggregations