16

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

1
  • Check out this answer if none of the above works for you. Commented Jul 25, 2022 at 20:58

6 Answers 6

49

Add below line in your application.properties

spring.data.mongodb.auto-index-creation: true
Sign up to request clarification or add additional context in comments.

5 Comments

this property needs to be enabled, after 3.0
You deserve a medal
Does this property create (re-create) the indexes every time the service is started or works like data migration and don't re-create is already present?
Idk if anyone else got it, but when I add that line in my application.properties. It gives me a UnsatisfiedDependencyException
Note that creating indices in this manner is not recommended. Especially if you are running an enterprise application that propagates through multiple environments, has reverts and rollbacks, you are much better off explicitly creating your own indices by integrating a migration tool like Mongock
5

I found the problem. I had another collection also with a url field marked as unqiue. I had to specify the name of the index on one of them otherwise it seems that it considered that the index already exists even though it was on two different collections

@Indexed(name = "meta_url_index_unique", unique = true)
private String url;

Comments

3

Edit: This answer was before the author updates his question

I believe you need to use the @Document annotation on top the class declaration

So your class should be

@Document
public class LinkMetaData {
@Indexed(unique = true)
private String url;
...
}

5 Comments

I did i just didn't include eveyrthing on top, ill update the question
Thanks for the clarification! Can you include the configuration you used to enable mongo
The configuration is on our config server I don't have access to it at this very moment. But it works with other collections in the same service that have unique indexes created by annotations.
I figured it out, posted an answer
really thanks bro, didint know @Document was obligatory
2

I Also had the same issue. The one that solved my issue was adding the index from the mongo db level

db.city.createIndex( { "name": 1 }, { unique: true } );

Comments

0

There's a high chance that you already have duplicates in your collection hence failing to create the index.

Check if you have duplicate URL values, if so, that should be your issue. Then you can drop the collection if it's not "PROD" data/ collection", then re-try, it should go through.

Comments

0

Did you created the repository class?

interface LinkMetaDataRepository : MongoRepository<LinkMetaData, String>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.