是否可以使用@Indexed或@CompoundIndex注释为特定的集合创建索引?



我有一个域对象,它上面有许多索引。我在Domain类上使用了@CompoundIndex,在一些类属性上使用了@Indexed注释,以允许Spring Data MongoDB自动创建必要的索引。

然而,我们现在想要将这个域存储在另一个集合中用于临时存档。但是,在另一个集合中,我们不需要所有这些索引。但是,Spring Data Mongo会创建它们。

下面是一个域对象的例子:

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

是否有任何方式来控制哪些集合获得索引,而不是使用spring注释和编写自己的代码来创建和确保索引?

当持久化到"另一个">

的例子:

@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;
}
}

检查索引:

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"
}
]