Search in sources :

Example 1 with IndexOptions

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;
    }
}
Also used : IndexOptions(com.mongodb.client.model.IndexOptions) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument)

Example 2 with IndexOptions

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));
}
Also used : IndexOptions(com.mongodb.client.model.IndexOptions) Document(org.bson.Document) Test(org.junit.Test)

Example 3 with IndexOptions

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));
}
Also used : IndexOptions(com.mongodb.client.model.IndexOptions) Document(org.bson.Document) Test(org.junit.Test)

Example 4 with IndexOptions

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));
}
Also used : IndexOptions(com.mongodb.client.model.IndexOptions) Document(org.bson.Document) Test(org.junit.Test)

Example 5 with IndexOptions

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));
}
Also used : IndexOptions(com.mongodb.client.model.IndexOptions) ArrayList(java.util.ArrayList) Document(org.bson.Document) Test(org.junit.Test)

Aggregations

IndexOptions (com.mongodb.client.model.IndexOptions)11 Document (org.bson.Document)11 Test (org.junit.Test)4 MongoDatabase (com.mongodb.client.MongoDatabase)3 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)2 ArrayList (java.util.ArrayList)2 OBJECT_ID (com.facebook.presto.mongodb.ObjectIdType.OBJECT_ID)1 ColumnHandle (com.facebook.presto.spi.ColumnHandle)1 SchemaNotFoundException (com.facebook.presto.spi.SchemaNotFoundException)1 SchemaTableName (com.facebook.presto.spi.SchemaTableName)1 Domain (com.facebook.presto.spi.predicate.Domain)1 Range (com.facebook.presto.spi.predicate.Range)1 TupleDomain (com.facebook.presto.spi.predicate.TupleDomain)1 BIGINT (com.facebook.presto.spi.type.BigintType.BIGINT)1 BOOLEAN (com.facebook.presto.spi.type.BooleanType.BOOLEAN)1 DOUBLE (com.facebook.presto.spi.type.DoubleType.DOUBLE)1 NamedTypeSignature (com.facebook.presto.spi.type.NamedTypeSignature)1 StandardTypes (com.facebook.presto.spi.type.StandardTypes)1 TIMESTAMP (com.facebook.presto.spi.type.TimestampType.TIMESTAMP)1 Type (com.facebook.presto.spi.type.Type)1