List of usage examples for com.mongodb DBCollection createIndex
public void createIndex(final DBObject keys, final DBObject options)
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Creates a sparse index with a unique constraint on a field, if one does not already exist on the specified collection. * Indexes created with this method are created in the background. <strong>Note:</strong> Do NOT use compound indexes to * create an sparse index, since the results are unexpected. * @param field - field that is used to index the elements * @param collection - collection where the index is created * @param descending - (optional) sort the elements of the index in descending order * @see <a href="http://docs.mongodb.org/manual/core/index-sparse/">MongoDB: Sparse Indexes</a> *///ww w. ja va 2s . c o m public void createSparseIndexWithUniqueConstraint(final String field, final String collection, final boolean descending) { checkArgument(isNotBlank(field), "Uninitialized or invalid field"); checkArgument(isNotBlank(collection), "Uninitialized or invalid collection"); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final DBCollection dbcol = db.getCollection(collection); dbcol.createIndex(new BasicDBObject(field, descending ? -1 : 1), new BasicDBObject(ImmutableMap.of("unique", true, "background", true, "sparse", true))); }
From source file:it.wami.map.mongodeploy.OsmToMongoDB.java
License:Apache License
/** * Create the index in the mongoDB.//from www . ja v a 2 s .co m * @param nodes the collection */ private static void createNodesIndex(DBCollection nodes) { BasicDBObject compound = new BasicDBObject(); compound.append("loc", "2dsphere"); BasicDBObject text = getTextIndex(); DBObject sparse = new BasicDBObject("sparse", true); BasicDBObject amenity = getAmenityIndex(); nodes.createIndex(text, sparse); nodes.createIndex(compound); nodes.createIndex(amenity, sparse); }
From source file:it.wami.map.mongodeploy.OsmToMongoDB.java
License:Apache License
/** * Create the index in the mongoDB./* w w w . j a v a 2s. c om*/ * @param ways the collection */ private static void createWaysIndex(DBCollection ways) { BasicDBObject compound = new BasicDBObject(); compound.append("loc", "2dsphere"); BasicDBObject text = getTextIndex(); DBObject sparse = new BasicDBObject("sparse", true); BasicDBObject amenity = getAmenityIndex(); ways.createIndex(amenity, sparse); ways.createIndex(text, sparse); ways.createIndex(compound); }
From source file:it.wami.map.mongodeploy.OsmToMongoDB.java
License:Apache License
/** * Create the index in the mongoDB.//from www.j a v a2 s. co m * @param relations the collection */ private static void createRelationsIndex(DBCollection relations) { BasicDBObject compound = new BasicDBObject(); compound.append("loc", "2dsphere"); compound.append("tags.route", 1); BasicDBObject amenity = getAmenityIndex(); DBObject sparse = new BasicDBObject("sparse", true); relations.createIndex(amenity, sparse); relations.createIndex(compound); }
From source file:nl.knaw.huygens.timbuctoo.storage.mongo.MongoDB.java
License:Open Source License
/** * Creates an execute on a set of fields, if one does not already exist, * using the specified options.//from w w w .j a v a2 s . co m */ public void createIndex(DBCollection collection, DBObject keys, DBObject options) throws StorageException { try { collection.createIndex(keys, options); } catch (MongoException e) { throw new StorageException(e); } }
From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoBlobStore.java
License:Apache License
private void initBlobCollection() { if (db.collectionExists(COLLECTION_BLOBS)) { return;/*from www . j a v a 2 s .co m*/ } DBCollection collection = getBlobCollection(); DBObject index = new BasicDBObject(); index.put(MongoBlob.KEY_ID, 1L); DBObject options = new BasicDBObject(); options.put("unique", Boolean.TRUE); collection.createIndex(index, options); }
From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoUtils.java
License:Apache License
/** * Forces creation of an index on a set of fields, if one does not already * exist.// w w w . j av a2 s .c o m * * @param collection the collection. * @param fields the name of the fields. * @param ascending {@code true} for an ascending, {@code false} for a * descending index. * @param unique whether values are unique. * @param sparse whether the index should be sparse. * @throws IllegalArgumentException if {@code fields} and {@code ascending} * arrays have different lengths. * @throws MongoException if the operation fails. */ static void createIndex(DBCollection collection, String[] fields, boolean[] ascending, boolean unique, boolean sparse) throws MongoException { checkArgument(fields.length == ascending.length); DBObject index = new BasicDBObject(); for (int i = 0; i < fields.length; i++) { index.put(fields[i], ascending[i] ? 1 : -1); } DBObject options = new BasicDBObject(); options.put("unique", unique); options.put("sparse", sparse); collection.createIndex(index, options); }
From source file:org.benjp.services.mongodb.MongoBootstrap.java
License:Open Source License
public void ensureIndexes() { String dbName = this.getDB().getName(); log.info("### ensureIndexes in " + dbName); BasicDBObject unique = new BasicDBObject(); unique.put("unique", true); unique.put("background", true); BasicDBObject notUnique = new BasicDBObject(); notUnique.put("unique", false); notUnique.put("background", true); DBCollection notifications = getDB().getCollection("notifications"); notifications.dropIndexes();//from w ww. j av a2 s.com notifications.createIndex(new BasicDBObject("user", 1), notUnique.append("name", "user_1").append("ns", dbName + ".notifications")); notifications.createIndex(new BasicDBObject("isRead", 1), notUnique.append("name", "isRead_1").append("ns", dbName + ".notifications")); BasicDBObject index = new BasicDBObject(); index.put("user", 1); index.put("categoryId", 1); index.put("category", 1); index.put("type", 1); // index.put("isRead", 1); notifications.createIndex(index, notUnique.append("name", "user_1_type_1_category_1_categoryId_1") .append("ns", dbName + ".notifications")); log.info("### notifications indexes in " + getDB().getName()); DBCollection rooms = getDB().getCollection("room_rooms"); rooms.dropIndexes(); rooms.createIndex(new BasicDBObject("space", 1), notUnique.append("name", "space_1").append("ns", dbName + ".room_rooms")); rooms.createIndex(new BasicDBObject("users", 1), notUnique.append("name", "users_1").append("ns", dbName + ".room_rooms")); rooms.createIndex(new BasicDBObject("shortName", 1), notUnique.append("name", "shortName_1").append("ns", dbName + ".room_rooms")); log.info("### rooms indexes in " + getDB().getName()); DBCollection coll = getDB() .getCollection(ChatServiceImpl.M_ROOM_PREFIX + ChatServiceImpl.M_ROOMS_COLLECTION); DBCursor cursor = coll.find(); while (cursor.hasNext()) { DBObject dbo = cursor.next(); String roomId = dbo.get("_id").toString(); DBCollection collr = getDB().getCollection(ChatServiceImpl.M_ROOM_PREFIX + roomId); collr.ensureIndex(new BasicDBObject("timestamp", 1), notUnique.append("name", "timestamp_1").append("ns", dbName + ".room_" + roomId)); collr.ensureIndex(new BasicDBObject("timestamp", -1), notUnique.append("name", "timestamp_m1").append("ns", dbName + ".room_" + roomId)); log.info("##### room index in " + roomId); } DBCollection users = getDB().getCollection("users"); users.dropIndexes(); users.createIndex(new BasicDBObject("token", 1), notUnique.append("name", "token_1").append("ns", dbName + ".users")); users.createIndex(new BasicDBObject("validity", -1), notUnique.append("name", "validity_m1").append("ns", dbName + ".users")); index = new BasicDBObject(); index.put("user", 1); index.put("token", 1); users.createIndex(index, unique.append("name", "user_1_token_1").append("ns", dbName + ".users")); index = new BasicDBObject(); index.put("user", 1); index.put("validity", -1); users.createIndex(index, unique.append("name", "user_1_validity_m1").append("ns", dbName + ".users")); index = new BasicDBObject(); index.put("validity", -1); index.put("isDemoUser", 1); users.createIndex(index, notUnique.append("name", "validity_1_isDemoUser_m1").append("ns", dbName + ".users")); users.createIndex(new BasicDBObject("user", 1), unique.append("name", "user_1").append("ns", dbName + ".users")); users.createIndex(new BasicDBObject("spaces", 1), notUnique.append("name", "spaces_1").append("ns", dbName + ".users")); log.info("### users indexes in " + getDB().getName()); log.info("### Indexes creation completed in " + getDB().getName()); }
From source file:org.exoplatform.chat.services.mongodb.MongoBootstrap.java
License:Open Source License
public void ensureIndexes() { String dbName = this.getDB().getName(); LOG.info("### ensureIndexes in " + dbName); BasicDBObject unique = new BasicDBObject(); unique.put("unique", true); unique.put("background", true); BasicDBObject notUnique = new BasicDBObject(); notUnique.put("unique", false); notUnique.put("background", true); DBCollection notifications = getDB().getCollection("notifications"); notifications.dropIndexes();// ww w . j av a2 s.c o m notifications.createIndex(new BasicDBObject("user", 1), notUnique.append("name", "user_1").append("ns", dbName + ".notifications")); notifications.createIndex(new BasicDBObject("isRead", 1), notUnique.append("name", "isRead_1").append("ns", dbName + ".notifications")); BasicDBObject index = new BasicDBObject(); index.put("user", 1); index.put("categoryId", 1); index.put("category", 1); index.put("type", 1); // index.put("isRead", 1); notifications.createIndex(index, notUnique.append("name", "user_1_type_1_category_1_categoryId_1") .append("ns", dbName + ".notifications")); LOG.info("### notifications indexes in " + getDB().getName()); DBCollection rooms = getDB().getCollection("room_rooms"); rooms.dropIndexes(); rooms.createIndex(new BasicDBObject("space", 1), notUnique.append("name", "space_1").append("ns", dbName + ".room_rooms")); rooms.createIndex(new BasicDBObject("users", 1), notUnique.append("name", "users_1").append("ns", dbName + ".room_rooms")); rooms.createIndex(new BasicDBObject("shortName", 1), notUnique.append("name", "shortName_1").append("ns", dbName + ".room_rooms")); LOG.info("### rooms indexes in " + getDB().getName()); DBCollection coll = getDB() .getCollection(ChatServiceImpl.M_ROOM_PREFIX + ChatServiceImpl.M_ROOMS_COLLECTION); DBCursor cursor = coll.find(); while (cursor.hasNext()) { DBObject dbo = cursor.next(); String roomId = dbo.get("_id").toString(); DBCollection collr = getDB().getCollection(ChatServiceImpl.M_ROOM_PREFIX + roomId); collr.ensureIndex(new BasicDBObject("timestamp", 1), notUnique.append("name", "timestamp_1").append("ns", dbName + ".room_" + roomId)); collr.ensureIndex(new BasicDBObject("timestamp", -1), notUnique.append("name", "timestamp_m1").append("ns", dbName + ".room_" + roomId)); LOG.info("##### room index in " + roomId); } DBCollection users = getDB().getCollection("users"); users.dropIndexes(); users.createIndex(new BasicDBObject("token", 1), notUnique.append("name", "token_1").append("ns", dbName + ".users")); users.createIndex(new BasicDBObject("validity", -1), notUnique.append("name", "validity_m1").append("ns", dbName + ".users")); index = new BasicDBObject(); index.put("user", 1); index.put("token", 1); users.createIndex(index, unique.append("name", "user_1_token_1").append("ns", dbName + ".users")); index = new BasicDBObject(); index.put("user", 1); index.put("validity", -1); users.createIndex(index, unique.append("name", "user_1_validity_m1").append("ns", dbName + ".users")); index = new BasicDBObject(); index.put("validity", -1); index.put("isDemoUser", 1); users.createIndex(index, notUnique.append("name", "validity_1_isDemoUser_m1").append("ns", dbName + ".users")); users.createIndex(new BasicDBObject("user", 1), unique.append("name", "user_1").append("ns", dbName + ".users")); users.createIndex(new BasicDBObject("spaces", 1), notUnique.append("name", "spaces_1").append("ns", dbName + ".users")); LOG.info("### users indexes in " + getDB().getName()); LOG.info("### Indexes creation completed in " + getDB().getName()); }
From source file:org.graylog2.metrics.MongoDbMetricsReporter.java
License:Open Source License
@Override public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) { final Date timestamp = new Date(clock.getTime()); List<DBObject> docs = Lists.newArrayListWithExpectedSize( gauges.size() + counters.size() + histograms.size() + meters.size() + timers.size()); collectGaugeReports(docs, gauges, timestamp); collectCounterReports(docs, counters, timestamp); collectHistogramReports(docs, histograms, timestamp); collectMeterReports(docs, meters, timestamp); collectTimerReports(docs, timers, timestamp); try {/* w w w . j a v a2s .co m*/ final DBCollection collection = mongoConnection.getDatabase().getCollection("graylog2_metrics"); // don't hang on to the data for too long. final BasicDBObject indexField = new BasicDBObject("timestamp", 1); final BasicDBObject indexOptions = new BasicDBObject("expireAfterSeconds", 5 * 60); collection.createIndex(indexField, indexOptions); collection.insert(docs, WriteConcern.UNACKNOWLEDGED); } catch (Exception e) { LOG.warn("Unable to write graylog2 metrics to mongodb. Ignoring this error.", e); } }