Java Code Examples for com.mongodb.client.MongoCollection.createIndex()

The following are Jave code examples for showing how to use createIndex() of the com.mongodb.client.MongoCollection class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: QDrill   File: MongoTestSuit.java   View Source Code Vote up 5 votes
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);
  }
  IndexOptions indexOptions = new IndexOptions().unique(true)
      .background(false).name(indexFieldName);
  Bson keys = new Document(indexFieldName, Integer.valueOf(1));
  mongoCollection.createIndex(keys, indexOptions);
}
 
Example 2
Project: spring-multitenancy   File: MongoBasicOperations.java   View Source Code Vote up 5 votes
@Override
public void ensureIndex(MongoCollection<Document> collection, String name, Document index, IndexOptions opts, boolean replace) {
	
	if(replace){
		collection.dropIndex(name);
	}
			
	collection.createIndex(index, opts);
}
 
Example 3
Project: tapir   File: SipMessageMongoCron.java   View Source Code Vote up 5 votes
private void ensureIndexes(String suffix) {
    MongoCollection index = db.getCollection("index_" + suffix);
    index.createIndex(new Document(Field.millis, 1));
    index.createIndex(new Document(Field.call_id, "hashed"));
    index.createIndex(new Document(Field.caller, "hashed"));
    index.createIndex(new Document(Field.callee, "hashed"));

    MongoCollection raw = db.getCollection("raw_" + suffix);
    raw.createIndex(new Document(Field.call_id, "hashed"));
}
 
Example 4
Project: osm-processor   File: MongoStore.java   View Source Code Vote up 5 votes
public void indexCollection(final String collectionName, final String field) {
    // Sanity checks
    if (StringUtils.isEmpty(collectionName)) {
        throw new IllegalArgumentException("count :: Collection name should not be blank");
    }

    // Collection
    MongoCollection<Document> collection = this.database.getCollection(collectionName);
    collection.createIndex(Indexes.descending(field));
}
 
Example 5
Project: dooo   File: MongodbDataAccess.java   View Source Code Vote up 5 votes
/**
 * 创建索引
 *
 * @param collectionName 集合
 * @param indexName      索引名
 * @param keys           键信息,如 securityCode:1
 * @param isUnique       是否唯一索引
 */
public void createIndex(String collectionName, String indexName, Map<String, Integer> keys, boolean isUnique) {
    MongoCollection collection = sMongoDatabase.getCollection(collectionName);
    IndexOptions indexOptions = new IndexOptions();
    indexOptions.background(true);
    indexOptions.unique(isUnique);
    indexOptions.name(indexName);
    Map<String, Object> keyIndexs = new HashMap<>(16);
    for (Map.Entry<String, Integer> entry : keys.entrySet()) {
        keyIndexs.put(entry.getKey(), entry.getValue() > 0 ? 1 : entry.getValue() == 0 ? 0 : -1);
    }
    collection.createIndex(new Document(keyIndexs), indexOptions);
}
 
Example 6
Project: nifi-nars   File: StoreInMongoIT.java   View Source Code Vote up 5 votes
@Test
public void testKeepIndexOutsideNamespace() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(new StoreInMongo());
    addMongoService(runner);
    MongoCollection<Document> collection = mongo.getMongoClient().getDatabase(MONGO_DATABASE_NAME).getCollection("index_test");
    Document index = new Document().append("_id", "hashed");
    collection.createIndex(index);
    runner.setProperty(MongoProps.DATABASE, MONGO_DATABASE_NAME);
    runner.setProperty(MongoProps.COLLECTION, "index_test");
    runner.assertValid();
    runner.run();
    Set<String> indexNames = AbstractMongoProcessor.getIndexNames(collection);
    String normalizedIndexName = AbstractMongoProcessor.normalizeIndexName("", index.toJson());
    assertTrue(indexNames.contains(normalizedIndexName));
}
 
Example 7
Project: nifi-nars   File: AbstractMongoProcessor.java   View Source Code Vote up 4 votes
protected void createIndex(String name, BasicDBList dbList, MongoCollection<Document> collection) {
    if (dbList.isEmpty() || name == null) {
        return;
    }

    BasicDBObject index = (BasicDBObject) dbList.get(0);
    IndexOptions options = new IndexOptions().name(name);
    if (dbList.size() > 1) {
        BasicDBObject opts = (BasicDBObject) dbList.get(1);
        if (opts.get("unique") != null && opts.get("unique") instanceof Boolean) {
            options = options.unique(opts.getBoolean("unique"));
        }
        if (opts.get("expireAfterSeconds") != null && opts.get("expireAfterSeconds") instanceof Integer) {
            options = options.expireAfter(Long.valueOf(opts.getInt("expireAfterSeconds")), TimeUnit.SECONDS);
        }
        if (opts.get("background") != null && opts.get("background") instanceof Boolean) {
            options = options.background(opts.getBoolean("background"));
        }
        if (opts.get("bits") != null && opts.get("bits") instanceof Integer) {
            options = options.bits(opts.getInt("bits"));
        }
        if (opts.get("bucketSize") != null && opts.get("bucketSize") instanceof Double) {
            options = options.bucketSize(opts.getDouble("bucketSize"));
        }
        if (opts.get("defaultLanguage") != null && opts.get("defaultLanguage") instanceof String) {
            options = options.defaultLanguage(opts.getString("defaultLanguage"));
        }
        if (opts.get("languageOverride") != null && opts.get("languageOverride") instanceof String) {
            options = options.languageOverride(opts.getString("languageOverride"));
        }
        if (opts.get("min") != null && opts.get("min") instanceof Double) {
            options = options.min(opts.getDouble("min"));
        }
        if (opts.get("max") != null && opts.get("max") instanceof Double) {
            options = options.max(opts.getDouble("max"));
        }
        if (opts.get("sparse") != null && opts.get("sparse") instanceof Boolean) {
            options = options.sparse(opts.getBoolean("sparse"));
        }
        if (opts.get("textVersion") != null && opts.get("textVersion") instanceof Integer) {
            options = options.textVersion(opts.getInt("textVersion"));
        }
        if (opts.get("version") != null && opts.get("version") instanceof Integer) {
            options = options.version(opts.getInt("version"));
        }
        if (opts.get("weights") != null && opts.get("weights") instanceof Bson) {
            options = options.weights((Bson) opts.get("weights"));
        }
    }
    collection.createIndex(new Document(index), options);
}