本文整理汇总了Java中com.mongodb.client.model.IndexOptions类的典型用法代码示例。如果您正苦于以下问题:Java IndexOptions类的具体用法?Java IndexOptions怎么用?Java IndexOptions使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IndexOptions类属于com.mongodb.client.model包,在下文中一共展示了IndexOptions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initCollections
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
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));
}
示例2: createIndex
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
@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;
}
示例3: internalSetupIndexes
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
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);
}
}
}
示例4: buildOptionsTest
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
@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"));
}
示例5: removeDuplicate
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
/**
* 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());
}
}
}
示例6: removeDuplicate
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
/**
* 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());
}
}
}
示例7: execute
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
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;
}
}
示例8: init
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
@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));
}
}
示例9: addNamedEventQueryToDB
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
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;
}
示例10: createIndex
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
/**
* 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));
}
}
示例11: createIndex
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
/**
*
* @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));
}
}
示例12: syncTtl
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
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));
}
}
}
示例13: create
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
@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));
;
});
}
示例14: shouldSyncTtlOnce
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
@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));
});
}
示例15: init
import com.mongodb.client.model.IndexOptions; //导入依赖的package包/类
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");
}