I have the following:
@Document(collection = "linkmetadata")
public class LinkMetaData {
@Indexed(unique = true)
private String url;
...
}
But whenever it creates the collection it doesn't create any index for the url field, it's like it just ignores the annotation. Any idea why this is?
Edit: no index is created upon insertion of data either. And when I try to fetch data for a specific url it throws an error that the url key is not unique if I entered the same url twice, but it doesn't care about inserting the unique key since there is no index..
Create a Single Index in MongoDB To start, select a database that already contains collections. Once you choose a database, use the createIndex() command in the shell to create a single index.
Indexes can be created by using the createIndex method. Indexes can be created on just one field or multiple field values. Indexes can be found by using the getIndexes method. Indexes can be removed by using the dropIndex for single indexes or dropIndexes for dropping all indexes.
MongoDB automatically determines whether to create a multikey index if the indexed field contains an array value; you do not need to explicitly specify the multikey type.
This annotation instructs the generator to index the element on which the annotated element is present or if it implements or extends from the annotated element. The stereotype is the fully qualified name of the annotated element. Consider the default Component annotation that is meta-annotated with this annotation.
use auto-index-creation: true
in your application properties.Add billow line in your application.properties
spring.data.mongodb.auto-index-creation: true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With