find*() 쿼리 사용 중 Mongodb 오류가 발생한 Springboot
다음의 에러가 표시됩니다.
com.aks. spring storage에 있습니다.SpringStorageApplication.main(SpringStorageApplication).java:22) [http/:na]
원인: org.springframework.data.mongodb.분류되지 않은 MongoDb예외:쿼리가 실패하고 오류 코드 2가 표시되며 오류 메시지 '필드 '로케일'이(가) 잘못되었습니다. 서버 localhost: 27017의 { local: "company }. 중첩된 예외는 com.mongodb입니다.MongoQueryException:쿼리가 실패하고 오류 코드 2가 표시되며 오류 메시지 '필드 '로케일'이(가) 잘못되었습니다: 서버 localhost: 27017의 { local: "company }.
이상한 점은 회사 컬렉션에서 "로케일"과 같은 변수를 사용하지 않는다는 것입니다.삽입하여 카운트를 취득할 수 있지만 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");
@Document(collation="company")
이것은 오타 및 문제의 원인인 것 같습니다.컬렉션 이름을 설정하려고 하지만 속성 대신 https://docs.mongodb.com/manual/reference/collation/을 사용합니다.
주석의 올바른 형식은 다음과 같습니다.
@Document(collection = "company")
public class Company {
// ...
}
또는 심플하게– 이후value
의 에일리어스입니다.collection
:
@Document("company")
public class Company {
// ...
}
컬렉션 이름을 완전히 생략할 수도 있습니다.이 경우 Spring은 클래스 이름을 컬렉션 이름으로 사용합니다.
@Document // name of collection wil be "company", as per class name
public class Company {
// ...
}
마지막 예는 컬렉션 이름을 명시적으로 지정하지 않았더라도 카운트 쿼리와 같이 이 작업이 작동한 이유입니다.
이 문제는 주석 @Document가 있는 주석 엔티티 클래스를 놓쳤을 때 발생할 수 있습니다.
잘못된 @Document(collation = "부서")
오른쪽 @Document(컬렉션 = "부서")
내 편에서 나는 사용했다.@Document(collection = "products")
일을 했어요.사용하다collection
대신collation
에@Document
.
언급URL : https://stackoverflow.com/questions/59532821/springboot-with-mongodb-error-while-using-find-query
'programing' 카테고리의 다른 글
JSON 문자열을 Javascript 배열로 변환 (0) | 2023.03.21 |
---|---|
Webpack-dev-server는 앱 페이지 대신 디렉토리 목록을 제공합니다. (0) | 2023.03.21 |
Python이 woocommerce rest api와 연결할 수 없습니다. (0) | 2023.03.21 |
Maven 및 Spring Boot - 해결할 수 없는 부모 POM - repo.spring.io (알 수 없는 호스트) (0) | 2023.03.21 |
Angular를 사용하여 앵커 태그 활성화/비활성화JS (0) | 2023.03.21 |