Akh*_*tro 11 mongodb spring-boot
我收到以下错误:
在 com.aks.springStorage.SpringStorageApplication.main(SpringStorageApplication.java:22) [classes/:na]
引起:org.springframework.data.mongodb.UncategorizedMongoDbException:查询失败,错误代码 2 和错误消息“字段'区域设置'在以下情况下无效:{ locale: "company" }' on server localhost:27017; 嵌套异常是 com.mongodb.MongoQueryException:查询失败,错误代码 2 和错误消息 'Field 'locale' is invalid in: { locale: "company" }' on server localhost:27017
奇怪的是我没有在公司集合中使用任何像“语言环境”这样的变量。我能够插入并能够获得计数,但是 findAll* 都没有工作,得到相同的错误。
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");
Run Code Online (Sandbox Code Playgroud)
Arc*_*hie 48
@Document(collation="company")
Run Code Online (Sandbox Code Playgroud)
这看起来像是一个错字和问题的原因。您尝试设置集合名称,而不是collection
使用collation
属性:https : //docs.mongodb.com/manual/reference/collation/
正确的注释形式是:
@Document(collection = "company")
public class Company {
// ...
}
Run Code Online (Sandbox Code Playgroud)
或者更简单——因为value
是 的别名collection
:
@Document("company")
public class Company {
// ...
}
Run Code Online (Sandbox Code Playgroud)
您甚至可以完全省略集合名称。在这种情况下,Spring 将使用类名作为集合名:
@Document // name of collection wil be "company", as per class name
public class Company {
// ...
}
Run Code Online (Sandbox Code Playgroud)
最后一个例子是为什么这对你有用,例如计数查询,即使你没有明确提供集合名称。
小智 12
当您错过带有注释 @Document 的注释实体类时,有时会发生这种情况
错误的@Document(collation = "department")
右@Document(集合=“部门”)
归档时间: |
|
查看次数: |
5355 次 |
最近记录: |