Reputation: 1486
I am using @indexed annotation in my document for the person while using spring boot and MongoDb.
I have already marked firstName to be used for indexing.
Can I mark other fields as well for index? The document is as mentioned below:
@Document
public class Person {
@Id private String id;
@Indexed(name = "first_name_index", direction = IndexDirection.DESCENDING)
private String firstName;
private String secondName;
private LocalDateTime dateOfBirth
}
Is it a good practice to mark more than one field as indexed?
Upvotes: 0
Views: 115
Reputation: 8203
Yes. You can index multiple fields as @Indexed
if your queries are orthogonal.
findByFirstName
and findByDateOfBirth
queries.@Indexed
annotation to firstName
@Indexed
annotation to dateOfBirth
findByFirstName
and findByFirstNameAndDateOfBirth
queries.@CompoundIndex(def = "{'findByFirstName': 1, 'dateOfBirth': 1}")
to public class Person
.Upvotes: 1