Java Code Examples for com.mongodb.DBCollection#createIndex()

The following examples show how to use com.mongodb.DBCollection#createIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoWrapper.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public DBCollection openMongoDb() throws UnknownHostException {

    	MongoClientURI dbUri = new MongoClientURI(dbUriStr_+"?socketTimeoutMS=180000");
	    mongoClient_ = new MongoClient(dbUri);

	    DB db = mongoClient_.getDB( dbName_ );
	    DBCollection coll = db.getCollection(collection_);
	    coll.createIndex(new BasicDBObject(index_, 1));  // create index on "i", ascending

	    return coll;

    }
 
Example 2
Source File: MongoTemplate.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures (creating if necessary) the index including the field(s) + directions; eg fields = "field1, -field2" ({field1:1, field2:-1})
 */
public void ensureIndex(String collName, String name, String fields, boolean unique, boolean dropDupsOnCreate) {

    BasicDBObject dbFields = parseFieldsString(fields);

    final BasicDBObjectBuilder keyOpts = new BasicDBObjectBuilder();
    if (name != null && name.length() != 0) {
        keyOpts.add("name", name);
    }
    if (unique) {
        keyOpts.add("unique", true);
        if (dropDupsOnCreate) {
            keyOpts.add("dropDups", true);
        }
    }

    final DBCollection dbColl = getCollection(getCollName(collName));

    final BasicDBObject opts = (BasicDBObject) keyOpts.get();
    if (opts.isEmpty()) {
        LOGGER.debug("Ensuring index for " + dbColl.getName() + " with keys:" + dbFields);
        dbColl.createIndex(dbFields);
    } else {
        LOGGER.debug("Ensuring index for " + dbColl.getName() + " with keys:" + fields + " and opts:" + opts);
        dbColl.createIndex(dbFields, opts);
    }
}
 
Example 3
Source File: MongoMapSet.java    From jelectrum with MIT License 5 votes vote down vote up
public MongoMapSet(DBCollection collection)
{
    this.collection = collection;

    //collection.ensureIndex(new BasicDBObject(KEY,1), new BasicDBObject());
    collection.createIndex(new BasicDBObject(KEY,1));
    /*Map<String, Object> m = new TreeMap<String, Object>();
    m.put(KEY,1);
    m.put("data",1);
    collection.ensureIndex(new BasicDBObject(m), new BasicDBObject("unique",true));*/
}
 
Example 4
Source File: SimpleMongoDBNamespaceManager.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public void createIndices(final DBCollection coll){
    coll.createIndex(PREFIX);
    coll.createIndex(NAMESPACE);
}
 
Example 5
Source File: SampleDataGenerator.java    From restfiddle with Apache License 2.0 4 votes vote down vote up
private void addIndexEntityAuth(){
DBCollection dbCollectionAuth = mongoTemplate.getCollection("EntityAuth");
dbCollectionAuth.createIndex(new BasicDBObject("expireAt", 1),new BasicDBObject("expireAfterSeconds", 0));
   }
 
Example 6
Source File: IndexingTask.java    From hvdf with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
	
	// Get a sorted list of slices 
	List<SliceDetails> slices = allocator.getCollectionSlices();
	Collections.sort(slices, SliceDetails.Comparators.MIN_TIME_DESCENDING);
	HashSet<String> currentCacheKeys = new HashSet<String>(this.indexCache.keySet());
	
	// For each collection look for the namespace in the cache	
	int order = 0;
	for(SliceDetails slice : slices){
		
		// Find the cache entry for this collection or create it
		currentCacheKeys.remove(slice.name);
		IndexRecord nsCache = this.indexCache.get(slice.name);
		if(nsCache == null){
			nsCache = new IndexRecord();
			this.indexCache.put(slice.name, nsCache);
		}
		
		// process if the cache is not complete for all configured keys
		if(nsCache.complete == false){
			
			nsCache.complete = true;
			for(PluginConfiguration indexConfig : indexList){
				
				// Index may be configured to not apply to n most recent
				int skips = indexConfig.get("skips", Integer.class, 0);
				if(order >= skips){
					
					try{
						// Now check if the index was already added
						DBObject proposed = indexConfig.get("keys", DBObject.class);
						if(nsCache.keySet.contains(proposed) == false){
							
							// Add the proposed index per config
							DBCollection coll = this.db.getCollection(slice.name);
							DBObject options = indexConfig.get("options", DBObject.class, new BasicDBObject());
							coll.createIndex(proposed, options);
							nsCache.keySet.add(proposed);
						}
					} catch(Exception ex){
						nsCache.complete = false;
						logger.warn("Exception in channel task", ex);
					}
					
				} else {						
					// Since this was skipped, mark as incomplete
					nsCache.complete = false;
				}					
			}
		}
		
		order++;
	}
	
	// The remainder of the keys in currentCacheKeys no longer exist
	// so they can be removed from the local cache also
	for(String togo : currentCacheKeys){
		this.indexCache.remove(togo);
	}
	
}
 
Example 7
Source File: MongoWrapper.java    From xDrip with GNU General Public License v3.0 3 votes vote down vote up
public DBCollection openMongoDb() throws UnknownHostException {

    	MongoClientURI dbUri = new MongoClientURI(dbUriStr_+"?socketTimeoutMS=180000");
	    mongoClient_ = new MongoClient(dbUri);

	    DB db = mongoClient_.getDB( dbName_ );
	    DBCollection coll = db.getCollection(collection_);
	    coll.createIndex(new BasicDBObject(index_, 1));  // create index on "i", ascending

	    return coll;

    }
 
Example 8
Source File: MongoWrapper.java    From xDrip-Experimental with GNU General Public License v3.0 3 votes vote down vote up
public DBCollection openMongoDb() throws UnknownHostException {

    	MongoClientURI dbUri = new MongoClientURI(dbUriStr_+"?socketTimeoutMS=180000");
	    mongoClient_ = new MongoClient(dbUri);

	    DB db = mongoClient_.getDB( dbName_ );
	    DBCollection coll = db.getCollection(collection_);
	    coll.createIndex(new BasicDBObject(index_, 1));  // create index on "i", ascending

	    return coll;

    }
 
Example 9
Source File: MongoWrapper.java    From xDrip with GNU General Public License v3.0 3 votes vote down vote up
public DBCollection openMongoDb() throws UnknownHostException {

    	MongoClientURI dbUri = new MongoClientURI(dbUriStr_); //?? thros
	    mongoClient_ = new MongoClient(dbUri);

	    DB db = mongoClient_.getDB( dbName_ );
	    DBCollection coll = db.getCollection(collection_);
	    coll.createIndex(new BasicDBObject(index_, 1));  // create index on "i", ascending

	    return coll;

    }