diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java index 535092bbb..a09e25418 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,8 @@ */ package org.springframework.data.mongodb.core; +import static org.springframework.data.domain.Sort.Direction.*; + import java.util.ArrayList; import java.util.List; @@ -22,7 +24,6 @@ import org.springframework.dao.DataAccessException; import org.springframework.data.mongodb.core.index.IndexDefinition; import org.springframework.data.mongodb.core.index.IndexField; import org.springframework.data.mongodb.core.index.IndexInfo; -import org.springframework.data.mongodb.core.query.Order; import org.springframework.util.Assert; import com.mongodb.DBCollection; @@ -34,9 +35,13 @@ import com.mongodb.MongoException; * * @author Mark Pollack * @author Oliver Gierke + * @author Komi Innocent */ public class DefaultIndexOperations implements IndexOperations { + private static final Double ONE = Double.valueOf(1); + private static final Double MINUS_ONE = Double.valueOf(-1); + private final MongoOperations mongoOperations; private final String collectionName; @@ -135,12 +140,17 @@ public class DefaultIndexOperations implements IndexOperations { Object value = keyDbObject.get(key); - if (Integer.valueOf(1).equals(value)) { - indexFields.add(IndexField.create(key, Order.ASCENDING)); - } else if (Integer.valueOf(-1).equals(value)) { - indexFields.add(IndexField.create(key, Order.DESCENDING)); - } else if ("2d".equals(value)) { + if ("2d".equals(value)) { indexFields.add(IndexField.geo(key)); + } else { + + Double keyValue = new Double(value.toString()); + + if (ONE.equals(keyValue)) { + indexFields.add(IndexField.create(key, ASC)); + } else if (MINUS_ONE.equals(keyValue)) { + indexFields.add(IndexField.create(key, DESC)); + } } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java index febdcdf60..f3e1b0338 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java @@ -90,6 +90,7 @@ import com.mongodb.WriteResult; * @author Amol Nayak * @author Patryk Wasik * @author Thomas Darimont + * @author Komi Innocent */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:infrastructure.xml") @@ -333,6 +334,43 @@ public class MongoTemplateTests { assertThat(field, is(IndexField.create("age", Direction.DESC))); } + /** + * @see DATAMONGO-746 + */ + @Test + public void testReadIndexInfoForIndicesCreatedViaMongoShellCommands() throws Exception { + + String command = "db." + template.getCollectionName(Person.class) + + ".ensureIndex({'age':-1}, {'unique':true, 'sparse':true})"; + template.indexOps(Person.class).dropAllIndexes(); + + assertThat(template.indexOps(Person.class).getIndexInfo().isEmpty(), is(true)); + factory.getDb().eval(command); + + List indexInfo = template.getCollection(template.getCollectionName(Person.class)).getIndexInfo(); + String indexKey = null; + boolean unique = false; + + for (DBObject ix : indexInfo) { + if ("age_-1".equals(ix.get("name"))) { + indexKey = ix.get("key").toString(); + unique = (Boolean) ix.get("unique"); + } + } + + assertThat(indexKey, is("{ \"age\" : -1.0}")); + assertThat(unique, is(true)); + + IndexInfo info = template.indexOps(Person.class).getIndexInfo().get(1); + assertThat(info.isUnique(), is(true)); + assertThat(info.isSparse(), is(true)); + + List indexFields = info.getIndexFields(); + IndexField field = indexFields.get(0); + + assertThat(field, is(IndexField.create("age", Direction.DESC))); + } + @Test public void testProperHandlingOfDifferentIdTypesWithMappingMongoConverter() throws Exception { testProperHandlingOfDifferentIdTypes(this.mappingTemplate);