Issue
I am saving customer records in Mongo DB, i am using Angular 6 as a front end. While saving, i am not sending Id value, so automatically Mongo is creating id and saving records.
I am using MongoRepository in Java for saving. But while doing "deleteById" or "findById", its not able to search or delete those records.
Can you help.
Angular Customer Model
export interface Customer {
id : string,
custId : number,
customerName : string,
email: string,
phone : string,
age: number,
city : string,
state : string,
createdDate : Date
}
User.service.ts
deleteCustomerData(id): Observable<Customer>{
console.log(this.deleteCustomerUrl + id);
return this.http.delete<Customer>(this.deleteCustomerUrl + id);
}
Java Controller
@DeleteMapping("/deleteCustomer/{id}")
public String deleteCustomerById(@PathVariable String id) {
//ObjectId objId = new ObjectId(id);
customerService.deleteCustomerById(id);
return "deleted customer by id"+ id;
}
Java Service
public void deleteCustomerById(String id) {
customerRepository.deleteById(id);
}
Java Model
@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 Repository
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>{
}
Solution
The issue is the data type of primary field need to be changed from String to ObjectId. Mongo db uses ObjecId as primary key type by default.
Answered By - GnanaJeyam
Answer Checked By - Candace Johnson (JavaFixing Volunteer)