Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

spring.data.mongodb.auto-index-creation: true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...