Is it possible to create Index for a specific Collection using @Indexed or @CompoundIndex annotation

Viewed 211

I have a domain object which has a number of indexes on it. I have used @CompoundIndex on the Domain class and @Indexed annotation on some of the class properties to allow Spring Data MongoDB to automatically create the necessary indexes.

However, we are now wanting to store this domain in another collection for temporary archive purposes. However, in this other collection, we don't need all of these indexes. However, Spring Data Mongo creates them.

Here is an example of the domain object:

@CompoundIndexes({
  @CompoundIndex(...),
  @CompoundIndex(...)})
public class Unit {
  @Id
  private ObjectId id;

  @Indexed
  private ObjectId ownerId;

  ...
}

Is there anyway to control which collections get the indexes other than not using the spring annotations and writing my own code to create and ensure indexes?

1 Answers

You can use MongoTemplate when persisting to "another" collection and MongoRepository for the rest.

Example:

@SpringBootApplication
public class AccessingDataMongodbApplication implements CommandLineRunner {

    @Autowired
    private CustomerRepository repository;

    @Autowired
    private MongoTemplate mongoTemplate;

    private static final String TEMP_COLLECTION = "customer_temp";

    public static void main(String[] args) {
        SpringApplication.run(AccessingDataMongodbApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

        final Customer cust1 = new Customer("David", "Gilmour");
        final Customer cust2 = new Customer("Roger", "Waters");

        mongoTemplate.save(cust1, TEMP_COLLECTION);
        mongoTemplate.save(cust2, TEMP_COLLECTION);

        repository.save(cust1);
        repository.save(cust2);
    }
}

@Document
@CompoundIndexes({
        @CompoundIndex(name = "cmp_idx", def = "{'firstname' : 1, 'lastname' : 1}"),
        @CompoundIndex(name = "compound_index", def = "{'firstName':1, 'salary':-1}")
})
class Customer {

    @Id
    public String id;
    private String firstName;
    public String lastName;
    private int salary;
    @Indexed
    public String email;

    public Customer(final String firstName, final String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

interface CustomerRepository extends MongoRepository<Customer, String> {

}

@Configuration
class MongoConf extends AbstractMongoClientConfiguration {

    @Override
    protected String getDatabaseName() {
        return "soq";
    }

    @Override
    public boolean autoIndexCreation() {
        return true;
    }
}

Check indexes:

rs0:PRIMARY> use index-test-db
switched to db index-test-db
rs0:PRIMARY> show collections
customer
customer_temp
rs0:PRIMARY>
rs0:PRIMARY>
rs0:PRIMARY> db.customer.find()
{ "_id" : ObjectId("602e98d31611dc3bf65f1a56"), "firstName" : "David", "lastName" : "Gilmour", "salary" : 0, "_class" : "com.
example.accessingdatamongodb.Customer" }
{ "_id" : ObjectId("602e98d31611dc3bf65f1a57"), "firstName" : "Roger", "lastName" : "Waters", "salary" : 0, "_class" : "com.e
xample.accessingdatamongodb.Customer" }
rs0:PRIMARY> db.customer.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "index-test-db.customer"
        },
        {
                "v" : 2,
                "key" : {
                        "firstname" : 1,
                        "lastname" : 1
                },
                "name" : "cmp_idx",
                "ns" : "index-test-db.customer"
        },
        {
                "v" : 2,
                "key" : {
                        "firstName" : 1,
                        "salary" : -1
                },
                "name" : "compound_index",
                "ns" : "index-test-db.customer"
        },
        {
                "v" : 2,
                "key" : {
                        "email" : 1
                },
                "name" : "email",
                "ns" : "index-test-db.customer"
        }
]
rs0:PRIMARY>
rs0:PRIMARY>
rs0:PRIMARY>
rs0:PRIMARY> db.customer_temp.find()
{ "_id" : ObjectId("602e98d31611dc3bf65f1a56"), "firstName" : "David", "lastName" : "Gilmour", "salary" : 0, "_class" : "com.
example.accessingdatamongodb.Customer" }
{ "_id" : ObjectId("602e98d31611dc3bf65f1a57"), "firstName" : "Roger", "lastName" : "Waters", "salary" : 0, "_class" : "com.e
xample.accessingdatamongodb.Customer" }
rs0:PRIMARY> db.customer_temp.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "index-test-db.customer_temp"
        }
]

Related