Spring Data: Unique field in MongoDB document
Asked Answered
M

6

20

let's say I have the following database entity:

@Document(collection = "users")
public class User {
    
    @Id
    private String id;

    private String firstname;

    private String lastname;

    private String email; 

}

How can I enforce the field email to be unique? That means MongoDB should check if a user record with this email address already exists when the application tries to save the entity.

Microcircuit answered 20/3, 2018 at 13:10 Comment(3)
@Indexed(unique = true)Pitfall
So it only works in combination with @Indexed?Microcircuit
I found this stackoverflow question/answer which helped me on this: https://mcmap.net/q/599765/-spring-boot-2-3-0-mongodb-library-does-not-create-indexes-automatically (None of the answers here helped me; running Springboot 3.0.2, Mongo)Peggypegma
Z
22

Mongodb needs to create and index a field in order to know whether the field is unique or not.

@Indexed(unique=true)
private String email;
Zygo answered 11/10, 2019 at 10:25 Comment(2)
I tried this and the mongo driver didn't do any enforcing to make this uniqueSanatorium
@Sanatorium : Did you create index on mongo side as well?Sewoll
L
9

First, use Indexed annotation above of your field in your model as shown below:

@Indexed(unique = true)
private String email;

Also, you should programmatically define your index. You should use the below code when defining your MongoTemplate.

mongoTemplate.indexOps("YOUR_COLLECTION_NAME").ensureIndex(new Index("YOUR_FEILD_OF_COLLECTION", Direction.ASC).unique());

For your case, you should use:

mongoTemplate.indexOps("users").ensureIndex(new Index("email", Direction.ASC).unique());
Leesen answered 21/9, 2020 at 11:38 Comment(1)
Why do we need to use the annotation if all the work is done on mongoTemplate?Disabled
B
9

This worked for me, but you have to delete your database and then re-run your application

 spring.data.mongodb.auto-index-creation=true
Brassiere answered 14/7, 2021 at 14:54 Comment(1)
@Moumen- How possible without deleting the database?Suricate
F
6

As of Spring Data MongoDB 3.0, automatic index creation is turned off by default. So basically, besides using @Indexed, you have to configure default indexing options. What you need to do is to make spring.data.mongodb.auto-index-creation=true in the application.properties file, and then @Indexed will work like a charm!

Formant answered 1/7, 2021 at 13:33 Comment(0)
A
1

You can try one of the below solutions, it worked for me.

Note: Please delete your db before you re-try with the below solutions.

Solution - 1

@Indexed(unique = true, background = true)
private String emailId;

Solution - 2

Add spring.data.mongodb.auto-index-creation=true to your application.properties file.

or

Add spring.data.mongodb.auto-index-creation:true to your yaml file

Antiar answered 31/1, 2022 at 11:19 Comment(0)
E
0

If anyone has a custom Mongo configuration -> spring.data.mongodb.auto-index-creation:true won't work. Instead try adding this to your MongoConfig:

       @Override
       public boolean autoIndexCreation() {
       return true;
       }

It solved the problem for me....

Evasive answered 14/6, 2022 at 8:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.