Example usage for com.mongodb DBCollection createIndex

List of usage examples for com.mongodb DBCollection createIndex

Introduction

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

Prototype

public void createIndex(final DBObject keys, final DBObject options) 

Source Link

Document

Creates an index on the field specified, if that index does not already exist.

Usage

From source file:org.hibernate.ogm.datastore.mongodb.impl.MongoDBSchemaDefiner.java

License:LGPL

public void createIndex(DB database, MongoDBIndexSpec indexSpec) {
    DBCollection collection = database.getCollection(indexSpec.getCollection());
    Map<String, DBObject> preexistingIndexes = getIndexes(collection);
    String preexistingTextIndex = getPreexistingTextIndex(preexistingIndexes);

    // if a text index already exists in the collection, MongoDB silently ignores the creation of the new text index
    // so we might as well log a warning about it
    if (indexSpec.isTextIndex() && preexistingTextIndex != null
            && !preexistingTextIndex.equalsIgnoreCase(indexSpec.getIndexName())) {
        throw log.unableToCreateTextIndex(collection.getName(), indexSpec.getIndexName(), preexistingTextIndex);
    }/*from www . j a v a  2s.  c om*/

    try {
        // if the index is already present and with the same definition, MongoDB simply ignores the call
        // if the definition is not the same, MongoDB throws an error, except in the case of a text index
        // where it silently ignores the creation
        collection.createIndex(indexSpec.getIndexKeysDBObject(), indexSpec.getOptions());
    } catch (MongoException e) {
        String indexName = indexSpec.getIndexName();
        if (e.getCode() == INDEX_CREATION_ERROR_CODE && !StringHelper.isNullOrEmptyString(indexName)
                && preexistingIndexes.containsKey(indexName)) {
            // The index already exists with a different definition and has a name: we drop it and we recreate it
            collection.dropIndex(indexName);
            collection.createIndex(indexSpec.getIndexKeysDBObject(), indexSpec.getOptions());
        } else {
            throw log.unableToCreateIndex(collection.getName(), indexName, e);
        }
    }
}

From source file:org.jmingo.JMingoTemplate.java

License:Apache License

/**
 * Creates index.//from ww  w  . j av a2  s .c  om
 *
 * @param collectionName the collection name to create index
 * @param index          the index to create
 */
public void ensureIndex(String collectionName, Index index) {
    Validate.notBlank(collectionName, "collectionName cannot be null or empty");
    Validate.notNull(index, "index cannot be null or empty");
    DBCollection dbCollection = mongoDBFactory.getDB().getCollection(collectionName);

    DBObject keys;
    DBObject options = null;
    if (MapUtils.isEmpty(index.getKeys())) {
        throw new IllegalArgumentException("necessary specify one or more keys to create an index");
    }
    keys = jacksonBsonMarshaller.marshall(BasicDBObject.class, index.getKeys());
    if (MapUtils.isNotEmpty(index.getOptions())) {
        options = jacksonBsonMarshaller.marshall(BasicDBObject.class, index.getOptions());
    }
    if (options != null) {
        dbCollection.createIndex(keys, options);
    } else {
        dbCollection.createIndex(keys);
    }
}

From source file:org.oncoblocks.centromere.mongodb.GenericMongoRepository.java

License:Apache License

/**
 * Creates an index on the desired field in the target collection.
 * //from  ww  w  .  ja v a2 s.com
 * @param field
 * @param direction
 * @param isUnique
 * @param isSparse
 */
public void createIndex(String field, Sort.Direction direction, boolean isUnique, boolean isSparse) {
    Integer dir = direction.equals(Sort.Direction.ASC) ? 1 : -1;
    DBObject index = new BasicDBObject(field, dir);
    DBObject options = new BasicDBObject();
    if (isSparse)
        options.put("sparse", true);
    if (isUnique)
        options.put("unique", true);
    DBCollection collection = mongoOperations.getCollection(mongoOperations.getCollectionName(model));
    collection.createIndex(index, options);
}

From source file:org.springframework.data.mongodb.core.DefaultIndexOperations.java

License:Apache License

public void ensureIndex(final IndexDefinition indexDefinition) {
    mongoOperations.execute(collectionName, new CollectionCallback<Object>() {
        public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
            DBObject indexOptions = indexDefinition.getIndexOptions();
            if (indexOptions != null) {
                collection.createIndex(indexDefinition.getIndexKeys(), indexOptions);
            } else {
                collection.createIndex(indexDefinition.getIndexKeys());
            }//w w w. j  a  va 2  s . c om
            return null;
        }
    });
}

From source file:org.teiid.translator.mongodb.MongoDBUpdateExecution.java

License:Open Source License

private void createIndex(DBCollection collection, KeyRecord record, boolean unique) {
    BasicDBObject key = new BasicDBObject();
    for (Column c : record.getColumns()) {
        key.append(getRecordName(c), 1);
    }//from w ww  .  j  a  v  a  2  s.c  o m
    BasicDBObject options = new BasicDBObject();
    options.put("name", record.getName()); //$NON-NLS-1$
    options.put("ns", collection.getName()); //$NON-NLS-1$
    if (unique) {
        options.put("unique", Boolean.TRUE); //$NON-NLS-1$
    }
    collection.createIndex(key, options);
}