Search in sources :

Example 1 with IndexField

use of org.springframework.data.mongodb.core.index.IndexField in project spring-data-mongodb by spring-projects.

the class GeoSpatial2DSphereTests method indexInfoIsCorrect.

// DATAMONGO-360
@Test
public void indexInfoIsCorrect() {
    IndexOperations operations = template.indexOps(Venue.class);
    List<IndexInfo> indexInfo = operations.getIndexInfo();
    assertThat(indexInfo.size(), is(2));
    List<IndexField> fields = indexInfo.get(0).getIndexFields();
    assertThat(fields.size(), is(1));
    assertThat(fields, hasItem(IndexField.create("_id", Direction.ASC)));
    fields = indexInfo.get(1).getIndexFields();
    assertThat(fields.size(), is(1));
    assertThat(fields, hasItem(IndexField.geo("location")));
}
Also used : IndexOperations(org.springframework.data.mongodb.core.index.IndexOperations) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) IndexField(org.springframework.data.mongodb.core.index.IndexField) Test(org.junit.Test)

Example 2 with IndexField

use of org.springframework.data.mongodb.core.index.IndexField in project spring-data-mongodb by spring-projects.

the class GeoSpatial2DTests method indexInfoIsCorrect.

// DATAMONGO-360
@Test
public void indexInfoIsCorrect() {
    IndexOperations operations = template.indexOps(Venue.class);
    List<IndexInfo> indexInfo = operations.getIndexInfo();
    assertThat(indexInfo.size(), is(2));
    List<IndexField> fields = indexInfo.get(0).getIndexFields();
    assertThat(fields.size(), is(1));
    assertThat(fields, hasItem(IndexField.create("_id", Direction.ASC)));
    fields = indexInfo.get(1).getIndexFields();
    assertThat(fields.size(), is(1));
    assertThat(fields, hasItem(IndexField.geo("location")));
}
Also used : IndexOperations(org.springframework.data.mongodb.core.index.IndexOperations) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) IndexField(org.springframework.data.mongodb.core.index.IndexField) Test(org.junit.Test)

Example 3 with IndexField

use of org.springframework.data.mongodb.core.index.IndexField in project spring-data-mongodb by spring-projects.

the class MongoTemplateTests method testEnsureIndex.

@Test
@SuppressWarnings("deprecation")
public void testEnsureIndex() throws Exception {
    Person p1 = new Person("Oliver");
    p1.setAge(25);
    template.insert(p1);
    Person p2 = new Person("Sven");
    p2.setAge(40);
    template.insert(p2);
    template.indexOps(Person.class).ensureIndex(new Index().on("age", Direction.DESC).unique());
    MongoCollection<org.bson.Document> coll = template.getCollection(template.getCollectionName(Person.class));
    List<org.bson.Document> indexInfo = new ArrayList<org.bson.Document>();
    coll.listIndexes().into(indexInfo);
    assertThat(indexInfo.size(), is(2));
    Object indexKey = null;
    boolean unique = false;
    for (org.bson.Document ix : indexInfo) {
        if ("age_-1".equals(ix.get("name"))) {
            indexKey = ix.get("key");
            unique = (Boolean) ix.get("unique");
            assertThat(ix.get("dropDups"), is(nullValue()));
        }
    }
    assertThat(((org.bson.Document) indexKey), IsMapContaining.<String, Object>hasEntry("age", -1));
    assertThat(unique, is(true));
    List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo();
    assertThat(indexInfoList.size(), is(2));
    IndexInfo ii = indexInfoList.get(1);
    assertThat(ii.isUnique(), is(true));
    assertThat(ii.isSparse(), is(false));
    List<IndexField> indexFields = ii.getIndexFields();
    IndexField field = indexFields.get(0);
    assertThat(field, is(IndexField.create("age", Direction.DESC)));
}
Also used : Index(org.springframework.data.mongodb.core.index.Index) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) IndexField(org.springframework.data.mongodb.core.index.IndexField) Test(org.junit.Test)

Example 4 with IndexField

use of org.springframework.data.mongodb.core.index.IndexField in project spring-data-mongodb by spring-projects.

the class MongoTemplateTests method testReadIndexInfoForIndicesCreatedViaMongoShellCommands.

// DATAMONGO-746
@Test
public void testReadIndexInfoForIndicesCreatedViaMongoShellCommands() throws Exception {
    String command = "db." + template.getCollectionName(Person.class) + ".createIndex({'age':-1}, {'unique':true, 'sparse':true}), 1";
    template.indexOps(Person.class).dropAllIndexes();
    assertThat(template.indexOps(Person.class).getIndexInfo().isEmpty(), is(true));
    factory.getDb().runCommand(new org.bson.Document("eval", command));
    ListIndexesIterable<org.bson.Document> indexInfo = template.getCollection(template.getCollectionName(Person.class)).listIndexes();
    org.bson.Document indexKey = null;
    boolean unique = false;
    MongoCursor<org.bson.Document> cursor = indexInfo.iterator();
    while (cursor.hasNext()) {
        org.bson.Document ix = cursor.next();
        if ("age_-1".equals(ix.get("name"))) {
            indexKey = (org.bson.Document) ix.get("key");
            unique = (Boolean) ix.get("unique");
        }
    }
    assertThat(indexKey, IsMapContaining.<String, Object>hasEntry("age", -1D));
    assertThat(unique, is(true));
    IndexInfo info = template.indexOps(Person.class).getIndexInfo().get(1);
    assertThat(info.isUnique(), is(true));
    assertThat(info.isSparse(), is(true));
    List<IndexField> indexFields = info.getIndexFields();
    IndexField field = indexFields.get(0);
    assertThat(field, is(IndexField.create("age", Direction.DESC)));
}
Also used : IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) IndexField(org.springframework.data.mongodb.core.index.IndexField) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 IndexField (org.springframework.data.mongodb.core.index.IndexField)4 IndexInfo (org.springframework.data.mongodb.core.index.IndexInfo)4 IndexOperations (org.springframework.data.mongodb.core.index.IndexOperations)2 Index (org.springframework.data.mongodb.core.index.Index)1