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)); }
public static <T extends OkraItem> void ensureIndexes(final Okra<T> okra, final MongoClient mongo, final String database, final String collection) { okra.getIndexDefs() .stream() .map(indexDef -> { final boolean ascending = indexDef.getOrdering() == null || indexDef.getOrdering().equals(Ordering.ASC); final Bson ordering = ascending ? Indexes.ascending(indexDef.getAttrs()) : Indexes.descending(indexDef.getAttrs()); return mongo .getDatabase(database) .getCollection(collection) .createIndex(ordering); }) .forEach(indexName -> LOGGER.info("Done. Index name: {}", indexName)); }
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)); }
private static Bson fromSmofIndexField(SmofIndexField field) { final String fieldName = field.name(); switch(field.type()) { case ASCENDING: return Indexes.ascending(fieldName); case DESCENDING: return Indexes.descending(fieldName); case TEXT: return Indexes.text(fieldName); } handleError(new IllegalArgumentException("Invalid index field.")); return null; }
private static Set<Bson> createRawIndexes(BsonDocument doc) { final Set<Bson> indexes = new LinkedHashSet<>(); final String name = doc.getString("name").getValue(); if(name.equals("_id_")) { indexes.add(Indexes.ascending(Element.ID)); } else { indexes.addAll(parseIndexName(name)); } return indexes; }
private static Bson nextIndex(StringTokenizer tokens) { final Bson index; final StringBuilder indexName = new StringBuilder(tokens.nextToken()); String indexTypeStr = tokens.nextToken(); IndexType indexT; while((indexT = IndexType.parse(indexTypeStr)) == null) { indexName.append("_").append(indexTypeStr); indexTypeStr = tokens.nextToken(); } switch(indexT) { case ASCENDING: index = Indexes.ascending(indexName.toString()); break; case DESCENDING: index = Indexes.descending(indexName.toString()); break; case TEXT: index = Indexes.text(indexName.toString()); break; default: handleError(new IllegalArgumentException("Invalid bson index")); index = null; break; } return index; }
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)); }
@Override public void start() throws IOException { MongoClientURI clientURI = new MongoClientURI(mongoURL); client = new MongoClient(clientURI); MongoDatabase db = client.getDatabase(clientURI.getDatabase()); collection = db.getCollection(clientURI.getCollection()).withWriteConcern(WriteConcern.JOURNALED); Bson index = Indexes.ascending(pKey); collection.createIndex(index); }
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); }
@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)); }
public QueryManager(ThreadFactory tf, MongoCollection<Document> db, ReportManager reportManager, I18n i18n, PluginContainer plugin) { this.db = db; this.reportManager = reportManager; this.i18n = i18n; storeExecuter = newSingleThreadExecutor(tf); queryShowExecutor = Sponge.getScheduler().createSyncExecutor(plugin); this.plugin = plugin; db.createIndex(Indexes.hashed("type")); db.createIndex(Indexes.descending("date")); db.createIndex(Indexes.ascending("data.location.Position_X", "data.location.Position_Z", "data.location.Position_Y")); db.createIndex(Indexes.hashed("data.location.WorldUuid")); }
private Bson fromIndexes(List<Bson> indexes) { if(indexes.size() == 1) { return indexes.get(0); } return Indexes.compoundIndex(indexes); }
@Override public void run(Environment environment, Namespace namespace, SamConfiguration configuration) throws Exception { final MongoDatabase database = configuration.getDbConnectionFactory().getDatabase(environment.lifecycle()); final MongoCollection<Document> servers = database.getCollection(Collections.SERVERS); servers.createIndexes(Lists.newArrayList( new IndexModel( ascending("fqdn"), new IndexOptions().unique(true).sparse(true) ), new IndexModel( compoundIndex(ascending("hostname"), ascending("environment")), new IndexOptions().unique(true) ), new IndexModel( compoundIndex(Indexes.text("hostname"), Indexes.text("environment"), Indexes.text("fqdn"), Indexes.text("description")), new IndexOptions().weights(new Document().append("hostname", 10).append("fqdn", 10).append("environment", 5)).defaultLanguage("sv") ) )); final MongoCollection<Document> applications = database.getCollection(Collections.APPLICATIONS); applications.createIndexes(Lists.newArrayList( new IndexModel( ascending("id"), new IndexOptions().unique(true) ), new IndexModel( compoundIndex(Indexes.text("id"), Indexes.text("name"), Indexes.text("description")), new IndexOptions().weights(new Document().append("id", 10).append("name", 10)).defaultLanguage("sv") ) )); final MongoCollection<Document> groups = database.getCollection(Collections.GROUPS); groups.createIndexes(Lists.newArrayList( new IndexModel( ascending("id"), new IndexOptions().unique(true) ), new IndexModel( ascending("tags") ), new IndexModel( compoundIndex(Indexes.text("id"), Indexes.text("name"), Indexes.text("description"), Indexes.text("tags")), new IndexOptions().weights(new Document().append("id", 10).append("name", 10).append("tags", 5)).defaultLanguage("sv") ) )); final MongoCollection<Document> assets = database.getCollection(Collections.ASSETS); assets.createIndexes(Lists.newArrayList( new IndexModel( ascending("id"), new IndexOptions().unique(true) ), new IndexModel( compoundIndex(Indexes.text("id"), Indexes.text("name"), Indexes.text("description")), new IndexOptions().weights(new Document().append("id", 10).append("name", 10)).defaultLanguage("sv") ) )); }
public MongoIndex ascending(String... keys) { this.bson = Indexes.ascending(keys); return this; }
public MongoIndex descending(String... keys) { this.bson = Indexes.descending(keys); return this; }
public MongoIndex geo2dsphere(String... keys) { this.bson = Indexes.geo2dsphere(keys); return this; }
public MongoIndex geo2d(String key) { this.bson = Indexes.geo2d(key); return this; }
public MongoIndex geoHaystack(String key, Bson additional) { this.bson = Indexes.geoHaystack(key, additional); return this; }
public MongoIndex text(String key) { this.bson = Indexes.text(key); return this; }
public MongoIndex hashed(String key) { this.bson = Indexes.hashed(key); return this; }
public MongoIndex add(MongoIndex mongoIndex) { indexModels.add(new IndexModel(Indexes.compoundIndex(mongoIndex.getBson()), mongoIndex)); return this; }