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:com.images3.data.impl.MongoDBAccessProvider.java

License:Apache License

private void initImageMetrics(DB db) {
    DBCollection coll = db.getCollection("ImageMetrics");
    coll.createIndex(
            new BasicDBObject().append("imagePlantId", 1).append("templateName", 1).append("second", 1),
            new BasicDBObject("unique", true));
}

From source file:com.impetus.client.mongodb.MongoDBClient.java

License:Apache License

/**
 * Creates the unique index gfs.//from   w w  w .  j a va2s.  c  om
 * 
 * @param coll
 *            the coll
 * @param id
 *            the id
 */
private void createUniqueIndexGFS(DBCollection coll, String id) {
    try {
        coll.createIndex(new BasicDBObject("metadata." + id, 1), new BasicDBObject("unique", true));
    } catch (MongoException ex) {
        throw new KunderaException("Error in creating unique indexes in " + coll.getFullName()
                + " collection on " + id + " field");
    }
}

From source file:com.impetus.client.mongodb.schemamanager.MongoDBSchemaManager.java

License:Apache License

private void indexColumn(IndexInfo indexInfo, DBCollection collection) {
    DBObject keys = new BasicDBObject();
    getIndexType(indexInfo.getIndexType(), keys, indexInfo.getColumnName());
    DBObject options = new BasicDBObject();
    if (indexInfo.getMinValue() != null) {
        options.put(MongoDBConstants.MIN, indexInfo.getMinValue());
    }//from w ww  .  j  av  a2 s.c  o  m
    if (indexInfo.getMaxValue() != null) {
        options.put(MongoDBConstants.MAX, indexInfo.getMaxValue());
    }

    if (indexInfo.getIndexType() != null && (indexInfo.getIndexType().toLowerCase()).equals("unique")) {
        options.put("unique", true);
    }
    collection.createIndex(keys, options);
    KunderaCoreUtils.printQuery("Create indexes on:" + keys, showQuery);
}

From source file:com.impetus.client.mongodb.schemamanager.MongoDBSchemaManager.java

License:Apache License

private void indexEmbeddedColumn(IndexInfo indexInfo, String embeddedColumnName, DBCollection collection) {
    DBObject keys = new BasicDBObject();
    getIndexType(indexInfo.getIndexType(), keys, embeddedColumnName + "." + indexInfo.getColumnName());
    DBObject options = new BasicDBObject();
    if (indexInfo.getMinValue() != null) {
        options.put(MongoDBConstants.MIN, indexInfo.getMinValue());
    }//w  ww.j  av  a2s .c om
    if (indexInfo.getMaxValue() != null) {
        options.put(MongoDBConstants.MAX, indexInfo.getMaxValue());
    }
    collection.createIndex(keys, options);
    KunderaCoreUtils.printQuery("Create indexes on:" + keys, showQuery);
}

From source file:com.impetus.client.mongodb.schemamanager.MongoDBSchemaManager.java

License:Apache License

private void createUniqueIndexGFS(DBCollection coll, String id) {
    try {//from ww w  . ja  v  a 2s .c  o m
        coll.createIndex(new BasicDBObject("metadata." + id, 1), new BasicDBObject("unique", true));
    } catch (MongoException ex) {
        throw new KunderaException("Error in creating unique indexes in " + coll.getFullName()
                + " collection on " + id + " field");
    }
}

From source file:com.mycompany.bean.PlytaService.java

public List<Plyta> searchByAutor(String DBName, String CollectionName, String paramAutor) throws Exception {
    List<Plyta> plyty = new ArrayList();
    try {//from w w w.ja v  a  2  s  .c  o m
        DBCollection collection = getConnection(DBName, CollectionName);
        BasicDBObject AutorIndex = new BasicDBObject();
        AutorIndex.put("autor", 1);
        collection.createIndex(AutorIndex, "IndexAutor");
        BasicDBObject query = new BasicDBObject();
        query.append("autor", paramAutor);
        DBCursor cur = collection.find(query);
        DBObject o;
        Plyta p;
        while (cur.hasNext()) {
            p = new Plyta();
            o = cur.next();
            p.setTytul((String) o.get("tytul"));
            p.setAutor((List<String>) o.get("autor"));
            p.setLiczbaUtworow((Integer) o.get("liczbaUtworow"));
            p.setWytwornia((List<String>) o.get("wytwornia"));
            p.setRokWydania((Integer) o.get("rokWydania"));
            p.setProducent((String) o.get("producent"));
            p.setGatunek((List<String>) o.get("gatunek"));
            p.setDlugosc((String) o.get("dlugosc"));
            p.setSingle((List<String>) o.get("single"));
            p.setNagrody((List<String>) o.get("nagrody"));
            p.setRodzajAlbumu((String) o.get("rodzajAlbumu"));
            p.setUtwory((List<Utwor>) o.get("utwory"));
            plyty.add(p);
        }
        log.log(Level.INFO, "Zapytanie wykonane poprawnie");
    } catch (Exception e) {
        log.log(Level.SEVERE, "Blad wykonania zapytania");
    }
    return plyty;
}

From source file:com.mycompany.bean.PlytaService.java

public List<Plyta> searchByGatunek(String DBName, String CollectionName, String paramGatunek) throws Exception {
    List<Plyta> plyty = new ArrayList();
    try {//from   w ww.j ava 2s. co  m
        DBCollection collection = getConnection(DBName, CollectionName);
        BasicDBObject GatunekIndex = new BasicDBObject();
        GatunekIndex.put("gatunek", 1);
        collection.createIndex(GatunekIndex, "IndexGatunek");
        BasicDBObject query = new BasicDBObject();
        query.append("gatunek", paramGatunek);
        DBCursor cur = collection.find(query);
        DBObject o;
        Plyta p;
        while (cur.hasNext()) {
            p = new Plyta();
            o = cur.next();
            p.setTytul((String) o.get("tytul"));
            p.setAutor((List<String>) o.get("autor"));
            p.setLiczbaUtworow((Integer) o.get("liczbaUtworow"));
            p.setWytwornia((List<String>) o.get("wytwornia"));
            p.setRokWydania((Integer) o.get("rokWydania"));
            p.setProducent((String) o.get("producent"));
            p.setGatunek((List<String>) o.get("gatunek"));
            p.setDlugosc((String) o.get("dlugosc"));
            p.setSingle((List<String>) o.get("single"));
            p.setNagrody((List<String>) o.get("nagrody"));
            p.setRodzajAlbumu((String) o.get("rodzajAlbumu"));
            p.setUtwory((List<Utwor>) o.get("utwory"));
            plyty.add(p);
        }
        log.log(Level.INFO, "Zapytanie wykonane poprawnie");
    } catch (Exception e) {
        log.log(Level.SEVERE, "Blad wykonania zapytania");
    }
    return plyty;
}

From source file:com.mycompany.bean.PlytaService.java

public List<Plyta> searchByUtwor(String DBName, String CollectionName, String paramUtwor) throws Exception {
    List<Plyta> plyty = new ArrayList();
    try {/*from   ww w. j a  va 2  s .  c  o m*/
        DBCollection collection = getConnection(DBName, CollectionName);
        BasicDBObject UtworIndex = new BasicDBObject();
        UtworIndex.put("utwory.nazwa", 1);
        collection.createIndex(UtworIndex, "IndexUtwor"); // tworzy index jeeli takiego nie ma
        BasicDBObject query = new BasicDBObject();
        query.append("utwory.nazwa", paramUtwor);
        DBCursor cur = collection.find(query);
        DBObject o;
        Plyta p;
        while (cur.hasNext()) {
            p = new Plyta();
            o = cur.next();
            p.setTytul((String) o.get("tytul"));
            p.setAutor((List<String>) o.get("autor"));
            p.setLiczbaUtworow((Integer) o.get("liczbaUtworow"));
            p.setWytwornia((List<String>) o.get("wytwornia"));
            p.setRokWydania((Integer) o.get("rokWydania"));
            p.setProducent((String) o.get("producent"));
            p.setGatunek((List<String>) o.get("gatunek"));
            p.setDlugosc((String) o.get("dlugosc"));
            p.setSingle((List<String>) o.get("single"));
            p.setNagrody((List<String>) o.get("nagrody"));
            p.setRodzajAlbumu((String) o.get("rodzajAlbumu"));
            p.setUtwory((List<Utwor>) o.get("utwory"));
            plyty.add(p);
        }
        log.log(Level.INFO, "Zapytanie wykonane poprawnie");
    } catch (Exception e) {
        log.log(Level.SEVERE, "Blad wykonania zapytania");
    }
    return plyty;
}

From source file:com.nec.strudel.tkvs.store.mongodb.MongodbLifecycle.java

License:Apache License

@Override
public void initialize() {
    DB db = mongoClient.getDB(dbName);/* w  w  w .j av a 2 s .c  o m*/
    DBCollection coll = db.getCollection(dbName);
    //create unique index on field docname,
    //this will have 2 effects:
    //1. Detect duplicate key upsertion when a
    //lot of initial transactions(meaning document
    //does not exist yet)
    //happen concurrently on the same entity group.
    //We catch duplicate key exception in transaction commit.
    //2. Faster retrieval of document using field docname
    coll.createIndex(new BasicDBObject(DOCNAME, 1), new BasicDBObject("unique", true));

    if (sharded) {
        //enable sharding
        db = mongoClient.getDB("admin");
        DBObject cmd = new BasicDBObject();
        cmd.put("enableSharding", dbName);
        db.command(cmd);

        cmd = new BasicDBObject();
        cmd.put("shardcollection", dbName + "." + dbName);
        cmd.put("key", new BasicDBObject(DOCNAME, 1));
        int chunk = getIntOpt(PROP_INIT_CHUNK, 0);
        if (chunk > 0) {
            cmd.put("numInitialChunks", chunk);
        }
        db.command(cmd);
    }
}

From source file:com.percona.LinkBench.MongoDBTestConfig.java

License:Apache License

@SuppressWarnings("serial")
static void createTestTables(DB db, String testDB) throws MongoClientException {

    // get handles to the collections
    DBCollection linkColl = getOrCreateCollection(db, linktable);
    DBCollection nodeColl = getOrCreateCollection(db, nodetable);
    DBCollection countColl = getOrCreateCollection(db, counttable);
    DBCollection transColl = getOrCreateCollection(db, transtable);

    // create indexes
    linkColl.createIndex(new BasicDBObject(new LinkedHashMap<String, Object>() {
        {/*www  .  j  a v  a2s.c  o m*/
            put("link_type", new Integer("1"));
            put("id1", new Integer("1"));
            put("id2", new Integer("1"));
        }
    }), new BasicDBObject("unique", true));
    linkColl.createIndex(new BasicDBObject(new LinkedHashMap<String, Object>() {
        {
            put("id1", new Integer("1"));
            put("link_type", new Integer("1"));
            put("visibility", new Integer("1"));
            put("time", new Integer("1"));
            put("id2", new Integer("1"));
            put("version", new Integer("1"));
            put("data", new Integer("1"));
        }
    }), new BasicDBObject("unique", true));
    countColl.createIndex(new BasicDBObject(new LinkedHashMap<String, Object>() {
        {
            put("id", new Integer("1"));
            put("link_type", new Integer("1"));
        }
    }), new BasicDBObject("unique", true));
    nodeColl.createIndex(new BasicDBObject(new LinkedHashMap<String, Object>() {
        {
            put("id", new Integer("1"));
        }
    }), new BasicDBObject("unique", true));
    transColl.createIndex(new BasicDBObject(new LinkedHashMap<String, Object>() {
        {
            put("link_type", new Integer("1"));
            put("id1", new Integer("1"));
            put("id2", new Integer("1"));
        }
    }));

}