Example usage for com.mongodb.client MongoCollection createIndex

List of usage examples for com.mongodb.client MongoCollection createIndex

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection createIndex.

Prototype

String createIndex(ClientSession clientSession, Bson keys);

Source Link

Document

Create an index with the given keys.

Usage

From source file:io.lumeer.storage.mongodb.dao.system.MongoUserDao.java

License:Open Source License

public void createUsersRepository() {
    database.createCollection(databaseCollectionName());

    MongoCollection<Document> userCollection = database.getCollection(databaseCollectionName());
    userCollection.createIndex(Indexes.ascending(UserCodec.EMAIL), new IndexOptions().unique(true));
}

From source file:io.lumeer.storage.mongodb.dao.system.MongoUserLoginDao.java

License:Open Source License

@Override
public void createLoginRepository() {
    if (database.getCollection(COLLECTION_NAME) == null) {
        database.createCollection(COLLECTION_NAME);

        MongoCollection<Document> groupCollection = database.getCollection(COLLECTION_NAME);
        groupCollection.createIndex(Indexes.ascending(UserLoginEvent.USER_ID, UserLoginEvent.DATE),
                new IndexOptions().unique(true));
    }/*from  www .j a  va 2s  .c om*/
}

From source file:io.seventyone.mongoutils.MongoServiceImplementation.java

License:Apache License

private void internalSetupIndexes(Class<?> entityClass, MongoCollection<Document> collection) {

    if (entityClass == null || collection == null) {
        return;/*w w w . j a  v  a 2  s.c  o m*/
    }

    MongoIndex[] indexes = entityClass.getAnnotationsByType(MongoIndex.class);
    if (indexes != null && indexes.length > 0) {
        for (MongoIndex index : indexes) {
            Document indexDocument = new Document();
            indexDocument.put(index.key(), index.direction());

            IndexOptions options = new IndexOptions();
            options.unique(index.unique());
            options.background(index.background());
            collection.createIndex(indexDocument, options);
        }
    }
}

From source file:net.netzgut.integral.mongo.internal.services.MongoServiceImplementation.java

License:Apache License

private void setupIndexes(Class<? extends Serializable> entityClass, MongoCollection<Document> collection) {
    if (entityClass == null) {
        log.error("Entity Class can't be null");
        throw new IllegalArgumentException("Entity Class can't be null");
    }/*  w w  w. j  a v  a  2 s.c om*/

    if (collection == null) {
        log.error("Collection can't be null");
        throw new IllegalArgumentException("Collection can't be null");
    }

    findAllAnnotations(Index.class, entityClass).stream().filter(index -> Objects.nonNull(index.value())) //
            .filter(index -> index.value().isEmpty() == false) //
            .forEach(index -> {
                Document indexDocument = new Document();
                indexDocument.put(index.value(), index.direction());

                IndexOptions options = new IndexOptions();
                options.unique(index.unique());
                options.background(index.background());
                collection.createIndex(indexDocument, options);
            });

}

From source file:org.apache.drill.exec.store.mongo.MongoTestSuit.java

License:Apache License

private static void createDbAndCollections(String dbName, String collectionName, String indexFieldName) {
    MongoDatabase db = mongoClient.getDatabase(dbName);
    MongoCollection<Document> mongoCollection = db.getCollection(collectionName);
    if (mongoCollection == null) {
        db.createCollection(collectionName);
        mongoCollection = db.getCollection(collectionName);
    }//from   w w w. j av  a 2s  . co m
    IndexOptions indexOptions = new IndexOptions().unique(true).background(false).name(indexFieldName);
    Bson keys = Indexes.ascending(indexFieldName);
    mongoCollection.createIndex(keys, indexOptions);
}

From source file:org.axonframework.mongo.eventsourcing.eventstore.AbstractMongoEventStorageStrategy.java

License:Apache License

@Override
public void ensureIndexes(MongoCollection<Document> eventsCollection,
        MongoCollection<Document> snapshotsCollection) {
    eventsCollection.createIndex(
            new BasicDBObject(eventConfiguration.aggregateIdentifierProperty(), ORDER_ASC)
                    .append(eventConfiguration.sequenceNumberProperty(), ORDER_ASC),
            new IndexOptions().unique(true).name("uniqueAggregateIndex"));

    eventsCollection.createIndex(/*  ww w  .  jav  a2s . c  o m*/
            new BasicDBObject(eventConfiguration.timestampProperty(), ORDER_ASC)
                    .append(eventConfiguration.sequenceNumberProperty(), ORDER_ASC),
            new IndexOptions().unique(false).name("orderedEventStreamIndex"));
    snapshotsCollection.createIndex(
            new BasicDBObject(eventConfiguration.aggregateIdentifierProperty(), ORDER_ASC)
                    .append(eventConfiguration.sequenceNumberProperty(), ORDER_ASC),
            new IndexOptions().unique(true).name("uniqueAggregateIndex"));
}

From source file:org.axonframework.mongo.eventsourcing.eventstore.documentpercommit.DocumentPerCommitStorageStrategy.java

License:Apache License

@Override
public void ensureIndexes(MongoCollection<Document> eventsCollection,
        MongoCollection<Document> snapshotsCollection) {
    super.ensureIndexes(eventsCollection, snapshotsCollection);
    //prevents duplicate commits
    eventsCollection.createIndex(
            new BasicDBObject(eventConfiguration().aggregateIdentifierProperty(), ORDER_ASC)
                    .append(commitEntryConfiguration.firstSequenceNumberProperty(), ORDER_ASC),
            new IndexOptions().unique(true).name("uniqueAggregateStartIndex"));
}

From source file:org.mongodb.morphia.IndexHelper.java

License:Apache License

void createIndex(final MongoCollection collection, final MappedClass mc, final Index index,
        final boolean background) {
    Index normalized = IndexBuilder.normalize(index);

    BsonDocument keys = calculateKeys(mc, normalized);
    com.mongodb.client.model.IndexOptions indexOptions = convert(normalized.options(), background);
    calculateWeights(normalized, indexOptions);

    collection.createIndex(keys, indexOptions);
}

From source file:org.radarcns.mongo.util.MongoHelper.java

License:Apache License

/**
 * Creates given index on the collection, if it is not already created for given collection.
 * Having indexes created in background prevents other operations being blocked.
 * @param collection mongoCollection to index.
 * @param index index to be created./*from  ww w. j  a  va  2s  . com*/
 */
private static void createIndexIfNotAvailable(MongoCollection<Document> collection, Bson index) {
    if (indexMap.containsKey(collection.getNamespace().getCollectionName())) {
        List<String> availableIndexes = indexMap.get(collection.getNamespace().getCollectionName());
        if (!availableIndexes.contains(index.toString())) {
            collection.createIndex(index, new IndexOptions().background(true));
            availableIndexes.add(index.toString());
        }

    } else {
        collection.createIndex(index, new IndexOptions().background(true));
        List<String> indexList = new ArrayList<>();
        indexList.add(index.toString());
        indexMap.put(collection.getNamespace().getCollectionName(), indexList);
    }
}

From source file:rapture.table.mongodb.MongoIndexHandler.java

License:Open Source License

private void createIt(IndexDefinition indexDefinition, boolean force, String indexName,
        MongoCollection<Document> collection) {
    // bug in mongo driver: need to set ns explicitly
    // String ns = collection.getDB() + "." + collection.getName();

    Document index = new Document();
    for (FieldDefinition f : indexDefinition.getFields()) {
        index.put(f.getName(), 1);/*ww  w  . j  a v  a2  s  . c  o  m*/
    }
    IndexOptions options = new IndexOptions().name(indexName).background(!force);
    collection.createIndex(index, options);
}