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:com.centurylink.mdw.mongo.MongoDocumentDb.java

License:Apache License

public static void createMongoDocIdIndex(String collectionName) {
    IndexOptions indexOptions = new IndexOptions().unique(true).background(true);
    MongoCollection<org.bson.Document> collection = mongoClient.getDatabase("mdw")
            .getCollection(collectionName);
    String indexName = collection.createIndex(Indexes.ascending("document_id"), indexOptions);
    LoggerUtil.getStandardLogger()//from ww  w  .  ja  v  a 2 s .c  om
            .mdwDebug("Created Index : " + indexName + " on collection : " + collectionName);
    collectionDocIdIndexed.putIfAbsent(collectionName, Boolean.valueOf(true));
}

From source file:com.epam.dlab.mongo.MongoDbConnection.java

License:Apache License

/**
 * Create index on billing collection./*  w w  w.  j  av  a 2s.co m*/
 *
 * @param indexName the name of index.
 * @param index     the index options.
 */
private void createBillingIndexes(String indexName, Bson index) {
    MongoCollection<Document> collection = database.getCollection(MongoConstants.COLLECTION_BILLING);
    IndexOptions options = new IndexOptions().name(MongoConstants.COLLECTION_BILLING + indexName);
    try {
        collection.createIndex(index, options);
    } catch (Exception e) {
        LOGGER.warn("Cannot create index {} on collection {}. {}", options.getName(),
                MongoConstants.COLLECTION_BILLING, e.getLocalizedMessage(), e);
    }
}

From source file:com.facebook.presto.mongodb.MongoSession.java

License:Apache License

private Document getTableMetadata(SchemaTableName schemaTableName) throws TableNotFoundException {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();

    MongoDatabase db = client.getDatabase(schemaName);
    MongoCollection<Document> schema = db.getCollection(schemaCollection);

    Document doc = schema.find(new Document(TABLE_NAME_KEY, tableName)).first();

    if (doc == null) {
        if (!collectionExists(db, tableName)) {
            throw new TableNotFoundException(schemaTableName);
        } else {// w  w  w .j  a  v  a 2 s  . c om
            Document metadata = new Document(TABLE_NAME_KEY, tableName);
            metadata.append(FIELDS_KEY, guessTableFields(schemaTableName));

            schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true));
            schema.insertOne(metadata);

            return metadata;
        }
    }

    return doc;
}

From source file:com.facebook.presto.mongodb.MongoSession.java

License:Apache License

private void createTableMetadata(SchemaTableName schemaTableName, List<MongoColumnHandle> columns)
        throws TableNotFoundException {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();

    MongoDatabase db = client.getDatabase(schemaName);
    Document metadata = new Document(TABLE_NAME_KEY, tableName);

    ArrayList<Document> fields = new ArrayList<>();
    if (!columns.stream().anyMatch(c -> c.getName().equals("_id"))) {
        fields.add(new MongoColumnHandle("_id", OBJECT_ID, true).getDocument());
    }/*from ww  w  . jav a 2s .  co  m*/

    fields.addAll(columns.stream().map(MongoColumnHandle::getDocument).collect(toList()));

    metadata.append(FIELDS_KEY, fields);

    MongoCollection<Document> schema = db.getCollection(schemaCollection);
    schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true));
    schema.insertOne(metadata);
}

From source file:com.helger.as2lib.partner.mongodb.MongoDBPartnershipFactory.java

License:Apache License

public MongoDBPartnershipFactory(final MongoCollection<Document> partnerships, final Logger logger) {
    this.logger = logger;
    partnerships.createIndex(new Document(NAME_KEY, Integer.valueOf(1)), new IndexOptions().unique(true));
    this.partnerships = partnerships;
}

From source file:io.djigger.collector.accessors.stackref.AbstractAccessor.java

License:Open Source License

private void createTimestampIndexWithOptions(MongoCollection<Document> collection, String attribute,
        IndexOptions options) {/* w w  w. j ava  2  s  .  c  o  m*/
    collection.createIndex(new Document(attribute, 1), options);
}

From source file:io.lumeer.storage.mongodb.dao.organization.MongoCompanyContactDao.java

License:Open Source License

@Override
public void createCompanyContactRepository() {
    if (database.getCollection(COMPANY_CONTACT_COLLECTION) == null) {
        database.createCollection(COMPANY_CONTACT_COLLECTION);

        MongoCollection<Document> companyContactCollection = database.getCollection(COMPANY_CONTACT_COLLECTION);
        companyContactCollection.createIndex(Indexes.ascending(CompanyContactCodec.ORGANIZATION_ID),
                new IndexOptions().unique(true));
    }/*from  www  .j  a v a2  s  .  com*/
}

From source file:io.lumeer.storage.mongodb.dao.organization.MongoPaymentDao.java

License:Open Source License

@Override
public void createPaymentRepository(final Organization organization) {
    database.createCollection(databaseCollectionName(organization));

    MongoCollection<Document> groupCollection = database.getCollection(databaseCollectionName(organization));
    groupCollection.createIndex(Indexes.ascending(PaymentCodec.PAYMENT_ID), new IndexOptions().unique(true));
    groupCollection.createIndex(Indexes.descending(PaymentCodec.DATE), new IndexOptions().unique(true));
    groupCollection.createIndex(Indexes.descending(PaymentCodec.START), new IndexOptions().unique(true));
    groupCollection.createIndex(Indexes.descending(PaymentCodec.VALID_UNTIL), new IndexOptions().unique(true));
}

From source file:io.lumeer.storage.mongodb.dao.project.MongoViewDao.java

License:Open Source License

@Override
public void createViewsRepository(Project project) {
    database.createCollection(databaseCollectionName(project));

    MongoCollection<Document> projectCollection = database.getCollection(databaseCollectionName(project));
    projectCollection.createIndex(Indexes.ascending(ViewCodec.CODE), new IndexOptions().unique(true));
    projectCollection.createIndex(Indexes.ascending(ViewCodec.NAME), new IndexOptions().unique(true));
    projectCollection.createIndex(Indexes.text(ViewCodec.NAME));
}

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

License:Open Source License

@Override
public void createGroupsRepository(Organization organization) {
    database.createCollection(databaseCollectionName(organization));

    MongoCollection<Document> groupCollection = database.getCollection(databaseCollectionName(organization));
    groupCollection.createIndex(Indexes.ascending(GroupCodec.NAME), new IndexOptions().unique(true));
}