我在 Mongo DB 中保存客户记录,我使用 Angular 6 作为前端。保存时,我没有发送 Id 值,因此 Mongo 会自动创建 id 并保存记录。
我在 Java 中使用 MongoRepository 进行保存。但是在执行“deleteById”或“findById”时,它无法搜索或删除这些记录。
你能帮我吗。
角度客户模型
export interface Customer {
id : string,
custId : number,
customerName : string,
email: string,
phone : string,
age: number,
city : string,
state : string,
createdDate : Date
}
用户服务.ts
deleteCustomerData(id): Observable<Customer>{
console.log(this.deleteCustomerUrl + id);
return this.http.delete<Customer>(this.deleteCustomerUrl + id);
}
Java 控制器
@DeleteMapping("/deleteCustomer/{id}")
public String deleteCustomerById(@PathVariable String id) {
//ObjectId objId = new ObjectId(id);
customerService.deleteCustomerById(id);
return "deleted customer by id"+ id;
}
Java 服务
public void deleteCustomerById(String id) {
customerRepository.deleteById(id);
}
Java 模型
@Document(collection="Customer")
public class Customer {
@Id
private String Id;
private String customerName;
private int age;
private String city;
private String state;
private int custId;
}
Java 存储库
package com.tivo.extract.config.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.tivo.extract.config.model.Customer;
public interface CustomerRepository extends MongoRepository<Customer, String>{
}