Example usage for org.springframework.data.mongodb.core.index Index Index

List of usage examples for org.springframework.data.mongodb.core.index Index Index

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.index Index Index.

Prototype

public Index(String key, Direction direction) 

Source Link

Usage

From source file:org.starfishrespect.myconsumption.server.business.repositories.repositoriesimpl.ValuesRepositoryImpl.java

@Override
public void init() throws DaoException {
    if (collectionName == null) {
        throw makeNoSensorException();
    }/*  w ww  .j a  v a  2s. c  om*/
    if (!mongoOperation.collectionExists(collectionName)) {
        mongoOperation.createCollection(collectionName);
        mongoOperation.indexOps(collectionName)
                .ensureIndex(new Index("timestamp", Sort.Direction.ASC).unique());

        // Distribute the database across multiple servers (useful in prod)
        /*            BasicDBObject shardCommand = new BasicDBObject("shardcollection", shardDbName + "." + collectionName);
                    shardCommand.put("key", new BasicDBObject("timestamp", 1));
                    CommandResult result = adminMongoOperation.executeCommand(shardCommand);
                    System.out.println(result.toString()); */
    }
}

From source file:com.traffitruck.service.MongoDAO.java

@Autowired
public MongoDAO(MongoTemplate mongoTemplate) {
    this.mongoTemplate = mongoTemplate;
    // Creates an index on the specified field if the index does not already exist
    mongoTemplate.indexOps(Load.class)
            .ensureIndex(new GeospatialIndex("sourceLocation").typed(GeoSpatialIndexType.GEO_2DSPHERE));
    mongoTemplate.indexOps(Load.class)
            .ensureIndex(new GeospatialIndex("destinationLocation").typed(GeoSpatialIndexType.GEO_2DSPHERE));
    mongoTemplate.indexOps(Truck.class).ensureIndex(new Index("licensePlateNumber", Direction.ASC).unique());
    mongoTemplate.indexOps(LoadsUser.class).ensureIndex(new Index("username", Direction.ASC).unique());
    mongoTemplate.indexOps(LoadsUser.class).ensureIndex(new Index("email", Direction.ASC).unique());
}

From source file:org.starfishrespect.myconsumption.server.business.repositories.repositoriesimpl.UserRepositoryImpl.java

@Override
public void init() {
    if (!mongoOperation.collectionExists(COLLECTION_NAME)) {
        mongoOperation.createCollection(COLLECTION_NAME);
        mongoOperation.indexOps(User.class).ensureIndex(new Index("name", Sort.Direction.ASC).unique());
    }//from   ww w. j  a  va  2 s  .  c om
}

From source file:org.springframework.integration.mongodb.store.AbstractConfigurableMongoDbMessageStore.java

@Override
public void afterPropertiesSet() throws Exception {
    if (this.mongoTemplate == null) {
        if (this.mappingMongoConverter == null) {
            this.mappingMongoConverter = new MappingMongoConverter(
                    new DefaultDbRefResolver(this.mongoDbFactory), new MongoMappingContext());
            this.mappingMongoConverter.setApplicationContext(this.applicationContext);
            List<Object> customConverters = new ArrayList<Object>();
            customConverters.add(new MessageToBinaryConverter());
            customConverters.add(new BinaryToMessageConverter());
            this.mappingMongoConverter.setCustomConversions(new CustomConversions(customConverters));
            this.mappingMongoConverter.afterPropertiesSet();
        }/*from  w w  w .j  a v  a2  s . c  o  m*/
        this.mongoTemplate = new MongoTemplate(this.mongoDbFactory, this.mappingMongoConverter);
    }

    this.messageBuilderFactory = IntegrationUtils.getMessageBuilderFactory(this.applicationContext);

    IndexOperations indexOperations = this.mongoTemplate.indexOps(this.collectionName);

    indexOperations.ensureIndex(new Index(MessageDocumentFields.MESSAGE_ID, Sort.Direction.ASC));

    indexOperations.ensureIndex(new Index(MessageDocumentFields.GROUP_ID, Sort.Direction.ASC)
            .on(MessageDocumentFields.MESSAGE_ID, Sort.Direction.ASC).unique());

    indexOperations.ensureIndex(new Index(MessageDocumentFields.GROUP_ID, Sort.Direction.ASC)
            .on(MessageDocumentFields.LAST_MODIFIED_TIME, Sort.Direction.DESC)
            .on(MessageDocumentFields.SEQUENCE, Sort.Direction.DESC));
}

From source file:org.springframework.session.data.mongo.AbstractMongoSessionConverter.java

/**
 * Method ensures that there is a TTL index on {@literal expireAt} field. It's has
 * {@literal expireAfterSeconds} set to zero seconds, so the expiration time is
 * controlled by the application.//from  www  . j  av  a 2s.co  m
 *
 * It can be extended in custom converters when there is a need for creating
 * additional custom indexes.
 * @param sessionCollectionIndexes {@link IndexOperations} to use
 */
protected void ensureIndexes(IndexOperations sessionCollectionIndexes) {
    List<IndexInfo> indexInfo = sessionCollectionIndexes.getIndexInfo();
    for (IndexInfo info : indexInfo) {
        if (EXPIRE_AT_FIELD_NAME.equals(info.getName())) {
            LOG.debug("TTL index on field " + EXPIRE_AT_FIELD_NAME + " already exists");
            return;
        }
    }
    LOG.info("Creating TTL index on field " + EXPIRE_AT_FIELD_NAME);
    sessionCollectionIndexes.ensureIndex(
            new Index(EXPIRE_AT_FIELD_NAME, Sort.Direction.ASC).named(EXPIRE_AT_FIELD_NAME).expire(0));
}