25

I am getting the following error:

at com.aks.springStorage.SpringStorageApplication.main(SpringStorageApplication.java:22) [classes/:na]
Caused by: org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 2 and error message 'Field 'locale' is invalid in: { locale: "company" }' on server localhost:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 2 and error message 'Field 'locale' is invalid in: { locale: "company" }' on server localhost:27017

Strange thing is I am not using any variable like "locale" in company collection. I am able to insert and able to get the count, but none of the findAll* are working, getting the same error.

public interface CompanyRepository extends MongoRepository<Company, String> {
    List<Company> findByName(String name);

    @Query("{'contact.address': ?0}")
    List<Company> findByAddress(String address);
}

@Document(collation = "company")
public class Company {
    private int id;
    private String name;
    private List<Product> products;
    private Contact contact;

    public Company(int id, String name, List<Product> products, Contact contact) {
        this.id = id;
        this.name = name;
        this.products = products;
        this.contact = contact;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }
}

// Client code:      
//this is working fine
int count = (int) companyRepo.count();

// Failing Here
companies = companyRepo.findByName("yy");
3
  • 5
    collation="company" seems like typo Commented Dec 30, 2019 at 16:06
  • @MadhavKumarJha thanks! it was a typo error, I changed to collection. it worked. Commented Dec 30, 2019 at 16:48
  • 2
    The elusive "this question is a typo but should stay because it's an infuriatingly easy auto-complete typo" question! Commented Jul 30, 2020 at 4:05

5 Answers 5

100
@Document(collation="company")

This looks like a typo and the cause of your issue. You try to set collection name, but instead of collection you use collation property: https://docs.mongodb.com/manual/reference/collation/

The correct form of annotation would be:

@Document(collection = "company")
public class Company {
    // ...
}

Or simpler – since value is an alias for collection:

@Document("company")
public class Company {
    // ...
}

You can even completely omit collection name. In that case Spring will use class name as collection name:

@Document // name of collection wil be "company", as per class name
public class Company {
    // ...
}

The last example is why this was working for you with e.g. count queries, even though you did not provide collection name explicitly.

3
  • 4
    this is issue with intellij AutoSuggest :) Commented Dec 21, 2021 at 18:24
  • 1
    Helps a lot, facing the same issue
    – Coney
    Commented Apr 27, 2022 at 14:34
  • Facing same issue. Thanks a lot..! Commented Jun 6 at 3:35
13

This sometime happens when you miss annotated entity class with annotation @Document

  • Wrong @Document(collation = "department")

  • Right @Document(collection = "department")

1
  • Hahah, this is exactly what got me :D
    – micartey
    Commented Mar 26 at 17:56
6

On my side i used @Document(collection = "products") and worked. Use collection instead of collation in @Document.

0

I use @Document(collection = "main class/name of database or collection") instead of collation and it works, for example @Document(collection ="student")

0

change @Document(collation = "company") to @Document(collection = "company")

Not the answer you're looking for? Browse other questions tagged or ask your own question.