Example usage for com.mongodb DBCollection dropIndex

List of usage examples for com.mongodb DBCollection dropIndex

Introduction

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

Prototype

public void dropIndex(final String indexName) 

Source Link

Document

Drops the index with the given name from this collection.

Usage

From source file:org.exoplatform.mongo.service.impl.MongoRestServiceImpl.java

License:Open Source License

@DELETE
@Path("/databases/{dbName}/collections/{collName}/indexes")
@Override//from ww  w  .j av  a2  s  . c  om
public Response deleteIndexes(@PathParam("dbName") String dbName, @PathParam("collName") String collName,
        @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context SecurityContext securityContext) {
    if (shutdown) {
        return Response.status(ServerError.SERVICE_UNAVAILABLE.code())
                .entity(ServerError.SERVICE_UNAVAILABLE.message()).build();
    }
    Response response = null;
    String user = null;
    try {
        Credentials credentials = authenticateAndAuthorize(headers, uriInfo, securityContext);
        user = credentials.getUserName();
        String dbNamespace = constructDbNamespace(credentials.getUserName(), dbName);
        if (mongo.getDatabaseNames().contains(dbNamespace)) {
            DB db = mongo.getDB(dbNamespace);
            authServiceAgainstMongo(db);
            if (db.getCollectionNames().contains(collName)) {
                DBCollection dbCollection = db.getCollection(collName);
                List<DBObject> indexInfos = dbCollection.getIndexInfo();
                List<String> indexNames = new ArrayList<String>();
                for (DBObject indexInfo : indexInfos) {
                    String indexName = (String) indexInfo.get("name");
                    if (FILTERED_INDEX.equals(indexName)) {
                        continue;
                    }
                    indexNames.add(indexName);
                    dbCollection.dropIndex(indexName);
                }
                response = Response.ok("Deleted indexes: " + indexNames).build();
            } else {
                response = Response.status(ClientError.NOT_FOUND.code())
                        .entity(collName + " does not exist in " + dbName).build();
            }
        } else {
            response = Response.status(ClientError.NOT_FOUND.code()).entity(dbName + " does not exist").build();
        }
    } catch (Exception exception) {
        response = lobException(exception, headers, uriInfo);
    } finally {
        updateStats(user, "deleteIndexes");
    }

    return response;
}

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);
    }//w  w  w .ja  v  a 2 s  .c  o  m

    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

/**
 * Drops index by index name./* w w  w  .j  av a 2 s.co  m*/
 *
 * @param collectionName the collection name to drop index
 * @param indexName      the index name to drop
 */
public void dropIndex(String collectionName, String indexName) {
    Validate.notBlank(collectionName, "collectionName cannot be null or empty");
    Validate.notBlank(indexName, "index name cannot be null or empty");

    DBCollection dbCollection = mongoDBFactory.getDB().getCollection(collectionName);
    dbCollection.dropIndex(indexName);
}

From source file:org.mongeez.dao.impl.MongeezDaoImpl.java

License:Apache License

/**
 * Removes indices that were generated by versions before 0.9.3, since they're not supported by MongoDB 2.4+
 *///from w ww.ja va  2 s  .  c om
private void dropObsoleteChangeSetExecutionIndices() {
    String indexName = "type_changeSetExecution_file_1_changeId_1_author_1_resourcePath_1";
    DBCollection collection = getMongeezCollection();
    for (DBObject dbObject : collection.getIndexInfo()) {
        if (indexName.equals(dbObject.get("name"))) {
            collection.dropIndex(indexName);
        }
    }
}

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

License:Apache License

public void dropIndex(final String name) {
    mongoOperations.execute(collectionName, new CollectionCallback<Void>() {
        public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
            collection.dropIndex(name);
            return null;
        }// w  w  w.j a v  a 2 s.co m
    });

}

From source file:rapture.sheet.mongodb.MongoCellStore.java

License:Open Source License

public void drop() {
    DBCollection collection = MongoDBFactory.getDB(instanceName).getCollection(tableName);
    collection.drop();/*from w  w w  .ja v a 2s  .c o m*/
    collection.dropIndex(KEY);
    collection.dropIndex(ROW);
    collection.dropIndex(COL);
}

From source file:rapture.sheet.mongodb.MongoMetaSheetStore.java

License:Open Source License

public void drop() {
    DBCollection collection = MongoDBFactory.getDB(instanceName).getCollection(tableName);
    collection.drop();
    collection.dropIndex(KEY);
}