private static void initCollections(MongoDatabase db) { try { db.createCollection("users"); db.createCollection("categories"); db.createCollection("feeds"); db.createCollection("licenses"); } catch (Exception e) { Log.w(TAG, "[attemptCreateCollections] " + e.getMessage()); } users = db.getCollection("users"); categories = db.getCollection("categories"); feeds = db.getCollection("feeds"); licenses = db.getCollection("licenses"); users.createIndex( Indexes.ascending(User.USERNAME), new IndexOptions().unique(true)); categories.createIndex( Indexes.ascending(Category.OWNER, Category.KEY), new IndexOptions().unique(true)); }
@Override public String createIndex(Bson arg0, IndexOptions arg1) { int writeSize = 0; OperationMetric metric = null; if (MongoLogger.GATHERER.isEnabled()) { List<String> keyValuePairs = Arrays.asList(arg0.toString()); String operationName = "Mongo : " + getNamespace().getCollectionName() + " : createIndex"; metric = startMetric(operationName, keyValuePairs); } String retVal = collection.createIndex(arg0, arg1); stopMetric(metric, writeSize); return retVal; }
private void internalSetupIndexes(Class<?> entityClass, MongoCollection<Document> collection) { if (entityClass == null || collection == null) { return; } MongoIndex[] indexes = entityClass.getAnnotationsByType(MongoIndex.class); if (indexes != null && indexes.length > 0) { for (MongoIndex index : indexes) { Document indexDocument = new Document(); indexDocument.put(index.key(), index.direction()); IndexOptions options = new IndexOptions(); options.unique(index.unique()); options.background(index.background()); collection.createIndex(indexDocument, options); } } }
@Test public void buildOptionsTest() { SimpleBindings options = new SimpleBindings(); SimpleBindings weights = new SimpleBindings(); weights.put("TEST_FIELD", 1); options.put("unique", true); options.put("weights", weights); IndexOptions buildOptions = JsApiUtils.buildOptions(new IndexOptions(), options); Assert.assertTrue(buildOptions.isUnique()); BasicDBObject weightsTarget = (BasicDBObject)buildOptions.getWeights(); Assert.assertEquals(1, weightsTarget.get("TEST_FIELD")); }
/** * removeDuplicate * by add a unique index */ private static void removeDuplicate(){ MongoClient client = new MongoClient("127.0.0.1"); MongoDatabase db = client.getDatabase("airvis"); MongoCollection preprocess = db.getCollection("pm_preProcess"); MongoCollection process = db.getCollection("pmProcess"); IndexOptions option = new IndexOptions(); option.unique(true); process.createIndex(new Document().append("time",1).append("code",1), option); MongoCursor cur = preprocess.find().iterator(); while(cur.hasNext()){ Document obj = (Document)cur.next(); try { process.insertOne(obj); }catch(MongoWriteException e){ //duplicate error System.out.println(obj.toString()); } } }
/** * removeDuplicate * by add a unique index */ private static void removeDuplicate(){ MongoClient client = new MongoClient("127.0.0.1"); MongoDatabase db = client.getDatabase("pm"); MongoCollection preprocess = db.getCollection("pm_preProcess"); MongoCollection process = db.getCollection("pmProcess"); IndexOptions option = new IndexOptions(); option.unique(true); process.createIndex(new Document().append("time",1).append("code",1), option); MongoCursor cur = preprocess.find().iterator(); while(cur.hasNext()){ Document obj = (Document)cur.next(); try { process.insertOne(obj); }catch(MongoWriteException e){ //duplicate error System.out.println(obj.toString()); } } }
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { MongoDatabase db = getMongoDatabase( _session, argStruct ); String collection = getNamedStringParam(argStruct, "collection", null); if ( collection == null ) throwException(_session, "please specify a 'collection' parameter"); cfData keys = getNamedParam(argStruct, "keys", null ); if ( keys == null ) throwException(_session, "please specify 'keys' parameter"); String index = getNamedStringParam(argStruct, "name", null ); if ( index == null ) throwException(_session, "please specify 'index' parameter"); try{ db.getCollection( collection ).createIndex( getDocument(keys), new IndexOptions().background( true ).unique( getNamedBooleanParam(argStruct, "unique", false) ) ); return cfBooleanData.TRUE; } catch (Exception me){ throwException(_session, me.getMessage()); return null; } }
@PostConstruct public void init() { MongoDatabase database = mongoClient.getDatabase(properties.getMongoClientDatabase()); counters = database.getCollection("counters"); timeseries = database.getCollection("timeseries"); checkFilled(); MongoUtils.checkCapped(database, "timeseries", size, maxDocuments, false); timeseries = database.getCollection("timeseries"); List<Document> indexes = Lists.newArrayList(database.getCollection("timeseries").listIndexes()); List<String> indexNames = indexes.stream().map(doc -> doc.getString("name")).collect(Collectors.toList()); if (!indexNames.contains(INDEX_NAME)) { log.warn("Index on field time and type is missing, creating it. Exisiting indexes: {}", indexes); database.getCollection("timeseries").createIndex(new Document("timestamp_hour", 1).append("type", 1), new IndexOptions().name(INDEX_NAME).unique(true)); } }
private boolean addNamedEventQueryToDB(String name, String description, PollParameters p) { MongoCollection<BsonDocument> namedEventQueryCollection = Configuration.mongoDatabase.getCollection("NamedEventQuery", BsonDocument.class); MongoCollection<BsonDocument> eventDataCollection = Configuration.mongoDatabase.getCollection("EventData", BsonDocument.class); BsonDocument existingDoc = namedEventQueryCollection.find(new BsonDocument("name", new BsonString(name))).first(); if (existingDoc == null) { BsonDocument bson = PollParameters.asBsonDocument(p); bson.put("name", new BsonString(name)); bson.put("description", new BsonString(description)); namedEventQueryCollection.insertOne(bson); } else { return false; } // Create Index with the given NamedEventQuery name and background option IndexOptions indexOptions = new IndexOptions().name(name).background(true); BsonDocument indexDocument = makeIndexObject(p); eventDataCollection.createIndex(indexDocument, indexOptions); Configuration.logger.log(Level.INFO, "NamedEventQuery: " + name + " is added to DB. "); return true; }
/** * Create TTL index * * @param idleTime idle time in seconds * @see https://docs.mongodb.com/manual/core/index-ttl/ */ private void createIndex(long idleTime) { try { this.sessions.createIndex( new Document(SESSION_TTL, 1), new IndexOptions() .expireAfter(idleTime, TimeUnit.SECONDS) .name(SESSION_INDEX_NAME)); } catch (MongoException ex) {//update idle time this.sessions.dropIndex(SESSION_INDEX_NAME); this.sessions.createIndex( new Document(SESSION_TTL, 1), new IndexOptions() .expireAfter(idleTime, TimeUnit.SECONDS) .name(SESSION_INDEX_NAME)); } }
/** * * @param dbName * @param collection * @param keys * @param options */ void createIndex( String dbName, String collection, BsonDocument keys, BsonDocument options) { if (options == null) { client .getDatabase(dbName) .getCollection(collection) .createIndex(keys); } else { // need to find a way to get IndexOptions from json IndexOptions io = new IndexOptions(); io.background(true); client .getDatabase(dbName) .getCollection(collection) .createIndex(keys, getIndexOptions(options)); } }
private void syncTtl() { if (!ttlSync.get()) { ttlSync.set(true); if (timeout <= 0) { return; } log.debug("creating session timeout index"); if (existsIdx(SESSION_IDX)) { Document command = new Document("collMod", collection) .append("index", new Document("keyPattern", new Document("_accessedAt", 1)) .append("expireAfterSeconds", timeout)); log.debug("{}", command); Document result = db.runCommand(command); log.debug("{}", result); } else { sessions.createIndex( new Document("_accessedAt", 1), new IndexOptions() .name(SESSION_IDX) .expireAfter(timeout, TimeUnit.SECONDS)); } } }
@SuppressWarnings("rawtypes") @Test public void create() throws Exception { new MockUnit(Session.class, MongoDatabase.class, MongoCollection.class) .expect(boot) .expect(saveSession) .expect(noIndexes) .expect(unit -> { MongoCollection collection = unit.get(MongoCollection.class); IndexOptions options = unit.constructor(IndexOptions.class) .build(); expect(options.name("_sessionIdx_")).andReturn(options); expect(options.expireAfter(300L, TimeUnit.SECONDS)).andReturn(options); expect(collection.createIndex(new Document("_accessedAt", 1), options)) .andReturn("idx"); }) .run(unit -> { new MongoSessionStore(unit.get(MongoDatabase.class), "sess", "5m") .create(unit.get(Session.class)); ; }); }
@SuppressWarnings("rawtypes") @Test public void shouldSyncTtlOnce() throws Exception { new MockUnit(Session.class, MongoDatabase.class, MongoCollection.class) .expect(boot) .expect(saveSession) .expect(noIndexes) .expect(saveSession) .expect(unit -> { MongoCollection collection = unit.get(MongoCollection.class); IndexOptions options = unit.constructor(IndexOptions.class) .build(); expect(options.name("_sessionIdx_")).andReturn(options); expect(options.expireAfter(60L, TimeUnit.SECONDS)).andReturn(options); expect(collection.createIndex(new Document("_accessedAt", 1), options)) .andReturn("idx"); }) .run(unit -> { MongoSessionStore mongodb = new MongoSessionStore(unit.get(MongoDatabase.class), "sess", "60"); mongodb.save(unit.get(Session.class)); mongodb.save(unit.get(Session.class)); }); }
private void init() { LOG.info(">>> init"); try { mongo = new MongoDbAccessor(serverDto.getAdminUser(), serverDto.getAdminPw(), serverDto.getHosts()); final MongoDatabase db = mongo.getMongoDatabase(serverDto.getDb()); profileCollection = db.getCollection(serverDto.getCollection()); if(profileCollection == null) { throw new IllegalArgumentException("Can't continue without profile collection for " + serverDto.getHosts()); } IndexOptions indexOptions = new IndexOptions(); indexOptions.background(true); LOG.info("Create index {ts:-1, lbl:1} in the background if it does not yet exists"); profileCollection.createIndex(new BasicDBObject("ts",-1).append("lbl", 1), indexOptions); LOG.info("Create index {adr:1, db:1, ts:-1} in the background if it does not yet exists"); profileCollection.createIndex(new BasicDBObject("adr",1).append("db",1).append("ts", -1), indexOptions); } catch (MongoException e) { LOG.error("Exception while connecting to: {}", serverDto.getHosts(), e); } LOG.info("<<< init"); }
protected final FluentFuture<Void> doIndex( final Constraints.Constraint fields, final IndexOptions options) { return submit(new Callable<Void>() { @Override public Void call() { collection().createIndex(convertToBson(fields), options); return null; } }); }
@Override public void beforeRun() { super.beforeRun(); IndexOptions firstNameOption = new IndexOptions(); firstNameOption.unique(false); personCollection.createIndex(new Document("firstName", 1), firstNameOption); personCollection.createIndex(new Document("personalNote", "text")); personCollection.insertMany(documents); }
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); }
private MongoIndexChange createChange (CompoundIndex index, boolean replace){ Document definition = Document.parse(index.def()); IndexOptions opts = new IndexOptions(); opts.name(index.name()); opts.unique(index.unique()); opts.sparse(index.sparse()); opts.background(index.background()); return new MongoIndexChange(index.name(), definition, opts, replace); }
@Override public void ensureIndex(MongoCollection<Document> collection, String name, Document index, IndexOptions opts, boolean replace) { if(replace){ collection.dropIndex(name); } collection.createIndex(index, opts); }
/** * 创建索引 * * @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); }
public AbstractOperation(MongoDbAccessor mongoDbAccessor, String db, String collection, String queriedField){ this.mongoDbAccessor = mongoDbAccessor; mongoCollection = mongoDbAccessor.getMongoDatabase(db).getCollection(collection); this.queriedField = queriedField; final IndexOptions options = new IndexOptions(); options.background(false); mongoCollection.createIndex(new BasicDBObject(queriedField, 1), options); minId = getMinMax(mongoDbAccessor, queriedField, true); maxId = getMinMax(mongoDbAccessor, queriedField, false); }
private void createIt(IndexDefinition indexDefinition, boolean force, String indexName, MongoCollection<Document> collection) { // bug in mongo driver: need to set ns explicitly // String ns = collection.getDB() + "." + collection.getName(); Document index = new Document(); for (FieldDefinition f : indexDefinition.getFields()) { index.put(f.getName(), 1); } IndexOptions options = new IndexOptions().name(indexName).background(!force); collection.createIndex(index, options); }
public static void createMongoDocIdIndex(String collectionName) { IndexOptions indexOptions = new IndexOptions().unique(true).background(true); MongoCollection<org.bson.Document> collection = mongoClient.getDatabase("mdw").getCollection(collectionName); String indexName = collection.createIndex(Indexes.ascending("document_id"), indexOptions); LoggerUtil.getStandardLogger().mdwDebug("Created Index : " + indexName + " on collection : " + collectionName); collectionDocIdIndexed.putIfAbsent(collectionName, Boolean.valueOf(true)); }
public MongoRequestRepository(MongoDatabase database, Long minutesToExpire) { this.requests = database.getCollection("requests"); try { this.requests.dropIndex("createdAt_1"); logger.info("Dropped index 'createdAt', creating a new one."); } catch (MongoCommandException e) { logger.info("Index for 'createdAt' doesn't exist, creating index."); } this.requests.createIndex(new Document("createdAt", 1), new IndexOptions().expireAfter(minutesToExpire, TimeUnit.MINUTES)); this.gson = new Gson(); }
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 = Indexes.ascending(indexFieldName); mongoCollection.createIndex(keys, indexOptions); }
@Override public Observable<String> createIndex(final Bson key, final IndexOptions options) { return RxObservables.create(Observables.observe(new Block<SingleResultCallback<String>>() { @Override public void apply(final SingleResultCallback<String> callback) { wrapped.createIndex(key, options, callback); } }), observableAdapter); }
@PostConstruct public void createIndexes() { if (!indexExists(User.class, CUser.email)) { this.getCollection(User.class).createIndex(Indexes.ascending(CUser.email), new IndexOptions().unique(true)); } if (!indexExists(PersistentLogin.class, CPersistentLogin.userId)) { this.getCollection(PersistentLogin.class) .createIndex(Indexes.ascending(CPersistentLogin.userId)); } }
@Before public void setUp() throws Exception { MongoDatabase db = mongo.getDatabase(DATABASE_NAME); db.createCollection(TEST_WRITE_COLLECTION); db.createCollection(UNIQUE_KEY_EXCEPTION_COLLECTION); testWriteCollection = db.getCollection(TEST_WRITE_COLLECTION); testWriteCollection.createIndex(Indexes.text("name"), new IndexOptions().unique(true)); }
private void PrepareSystem(POCTestOptions testOpts,POCTestResults results) { MongoDatabase db; MongoCollection<Document> coll; //Create indexes and suchlike db = mongoClient.getDatabase(testOpts.databaseName); coll = db.getCollection(testOpts.collectionName); if(testOpts.emptyFirst) { coll.drop(); } for(int x=0;x<testOpts.secondaryidx;x++) { coll.createIndex(new Document("fld"+x,1)); } if (testOpts.fulltext) { IndexOptions options = new IndexOptions(); options.background(true); BasicDBObject weights = new BasicDBObject(); weights.put("lorem", 15); weights.put("_fulltext.text", 5); options.weights(weights); Document index = new Document(); index.put("$**", "text"); coll.createIndex(index, options); } results.initialCount = coll.count(); //Now have a look and see if we are sharded //And how many shards and make sure that the collection is sharded if(! testOpts.singleserver) { ConfigureSharding(testOpts); } }
public void close() { catCollection.createIndex(new Document("pid", 1), new IndexOptions().unique(false)); catCollection.createIndex(new Document("title", 1), new IndexOptions().unique(false)); catCollection.createIndex(new Document("parent", 1), new IndexOptions().unique(false)); pageCollection.createIndex(new Document("cat", 1), new IndexOptions().unique(false)); }
@Override public Publisher<String> createIndex(final Bson key, final IndexOptions options) { return new ObservableToPublisher<String>(observe(new Block<SingleResultCallback<String>>() { @Override public void apply(final SingleResultCallback<String> callback) { wrapped.createIndex(key, options, callback); } })); }
@Override public Publisher<String> createIndex(final ClientSession clientSession, final Bson key, final IndexOptions options) { return new ObservableToPublisher<String>(observe(new Block<SingleResultCallback<String>>() { @Override public void apply(final SingleResultCallback<String> callback) { wrapped.createIndex(clientSession, key, options, callback); } })); }
public void createRequiredUniqueIndex(MongoCollection<Document> collection) { collection.createIndex(new Document() .append(ChangeEntry.KEY_CHANGEID, 1) .append(ChangeEntry.KEY_AUTHOR, 1), new IndexOptions().unique(true) ); }
public static void createIndex(final MongoCollection<Document> collection, final Document keys, final IndexOptions options) { LOG.debug("createIndex: entry, collection={}, keys={}, options={}", MongoUtil.fullName(collection), keys.toJson(), toJson(options)); collection.createIndex(keys, options); LOG.debug("createIndex: exit"); }
/** * Prepares the data structure for this Entity class in the given database, this means creating declared * indexes etc. */ private MongoCollection<? extends Entity> prepareEntity( final EntityProperties properties ) { // TODO need to add verification that index field matches existing property Class<? extends Entity> clazz = properties.getEntityClass(); Collection c = clazz.getAnnotation( Collection.class ); MongoCollection<? extends Entity> coll = EntityCodec.getCollectionFor( db, properties ); if ( c != null && c.indexes() != null ) { LOG.debug( "Entity class {} has indexes, ensuring that MongoDB is setup", properties.getEntityClass() ); for ( Index index : c.indexes() ) { IndexOptions options = new IndexOptions(); if ( index.unique() ) { options.unique( true ); } if ( isNotEmpty( index.name() ) ) { options.name( index.name() ); } Document indxFields = new Document(); for ( IndexField field : index.value() ) { checkNotNull( properties.getProperty( field.field() ), "Index field '%s' for index '%s' does not exist for %s", field.field(), index.name(), clazz ); indxFields.put( field.field(), ( field.order() == IndexField.Ordering.ASC ) ? OrderBy.ASC.getIntRepresentation() : OrderBy.DESC.getIntRepresentation() ); } LOG.debug( "Creating index {} for Entity class {}", options.getName(), properties.getEntityClass() ); coll.createIndex( indxFields, options ); } } return coll; }
public MongoIndexChange(String name, Document definition, IndexOptions options, boolean replace){ this.name = name; this.definition = definition; this.options = options; this.replace = replace; }
public IndexOptions getOptions() { return options; }