Example usage for org.springframework.data.mongodb.core.mapping CustomCollectionWithIndex CustomCollectionWithIndex

List of usage examples for org.springframework.data.mongodb.core.mapping CustomCollectionWithIndex CustomCollectionWithIndex

Introduction

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

Prototype

public CustomCollectionWithIndex(String name) 

Source Link

Usage

From source file:org.springframework.data.mongodb.core.mapping.MappingTests.java

@Test
public void testIndexesCreatedInRightCollection() {
    CustomCollectionWithIndex ccwi = new CustomCollectionWithIndex("test");
    template.insert(ccwi);/*  w  ww . j a v  a  2s  .c  o m*/

    assertTrue(template.execute("foobar", new CollectionCallback<Boolean>() {
        public Boolean doInCollection(DBCollection collection) throws MongoException, DataAccessException {
            List<DBObject> indexes = collection.getIndexInfo();
            for (DBObject dbo : indexes) {
                if (dbo.get("name") != null && dbo.get("name") instanceof String
                        && ((String) dbo.get("name")).startsWith("name")) {
                    return true;
                }
            }
            return false;
        }
    }));

    DetectedCollectionWithIndex dcwi = new DetectedCollectionWithIndex("test");
    template.insert(dcwi);

    assertTrue(
            template.execute(MongoCollectionUtils.getPreferredCollectionName(DetectedCollectionWithIndex.class),
                    new CollectionCallback<Boolean>() {
                        public Boolean doInCollection(DBCollection collection)
                                throws MongoException, DataAccessException {
                            List<DBObject> indexes = collection.getIndexInfo();
                            for (DBObject dbo : indexes) {
                                if (dbo.get("name") != null && dbo.get("name") instanceof String
                                        && ((String) dbo.get("name")).startsWith("name")) {
                                    return true;
                                }
                            }
                            return false;
                        }
                    }));
}