Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot / mongo wont create index with the index annotation

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..

like image 840
Jonas B Avatar asked Oct 26 '18 10:10

Jonas B


People also ask

Is it possible to create an index in the Mongo shell?

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.

How do you create an index on a field in MongoDB?

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.

Does MongoDB create index automatically?

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.

What is indexed annotation in spring boot?

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.


1 Answers

use auto-index-creation: true in your application properties.Add billow line in your application.properties

spring.data.mongodb.auto-index-creation: true
like image 85
Dipak Avatar answered Nov 26 '22 07:11

Dipak