0

Working with MongoDB, I decided username should be unique. Okay, I use @Indexed(unique = true) (application runs fine), but @Indexed(unique = true) isn't working. I can still add 2 users with the same username.

Source (Spring Data: Unique field in MongoDB document) tells me to put spring.data.mongodb.auto-index-creation=true in my application.properties. (also tried putting it in application.yml, gave same error)

Later I also realized the @Size annotation from the jakarta.validation-api doesn't work.

User model:

import org.bson.types.Binary;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import javax.persistence.Id;
import javax.validation.constraints.Size;
import java.util.List;

@Document(collection = "users")
public class User {

    @Id
    private String id;

    private Binary profilePicture;

    private String bio;

    @Size(max = 20)
    @Indexed(unique = true)
    private String username;

    private String password;

    private List<Integer> kweets;

    private List<User> followers;

    private List<User> following;
}

Repository (is just standard):

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends MongoRepository<User, String> {
}

pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>jakarta.validation</groupId>
        <artifactId>jakarta.validation-api</artifactId>
    </dependency>
</dependencies>

Id's do get auto-generated though, which means @Id does work. Where could the problem lay, at first I thought it was just @Indexed, but turns out @Size doesn't work either.

zhrgci
  • 584
  • 1
  • 6
  • 25
  • "etc.". Can you post the full stack trace? I guess some root cause is probably pointing the exactly cause of the failure. – Matheus Mar 14 '22 at 12:43
  • In your error `Cannot create index for 'username' in collection 'users' with keys 'Document{{username=1}}' and options 'Document{{name=username, unique=true}}'. Index already defined as 'IndexInfo [indexFields=[IndexField [ key: username, direction: ASC, type: DEFAULT, weight: NaN]], name=username, unique=false, sparse=false, language=, ....: Command failed with error 86 (IndexKeySpecsConflict): 'An existing index has the same name as the requested index. When index names are not specified, they are auto generated and can cause conflicts. ` – varman Mar 14 '22 at 14:34
  • @varman I'm not sure what that means exactly (because I have no IndexInfo), but there were 4 indexes in my MongoDB (id, followers.username, following.username, username). I deleted the 2 that were too much, but still same error when I add the line to my app.prop. – zhrgci Mar 14 '22 at 14:45
  • Did you clean up your collection? hope you used http://mageddo.com/tools/yaml-converter to convert application.properties to yaml – varman Mar 14 '22 at 14:47
  • Yes, there were only 2 users in my collection. I also just tried the tool and tried it in yml again, same error. The application doesn't stop instantly, it shows a warning first and then it crashes (It's just the error message, but instead it's labelled a warning) – zhrgci Mar 14 '22 at 14:56

1 Answers1

0

It's fixed. Idk what it was, I stashed all the changes I made and it still works. I didn't change anything and it suddenly worked...

zhrgci
  • 584
  • 1
  • 6
  • 25