[英]How to delete record in MongoDB using Spring Data
I want to delete an record based on Id in Spring.我想在 Spring 中删除基于 Id 的记录。 but in database id value is object EX:-但在数据库 id 值是对象 EX:-
id: Object(34562341112313)
You do like this: 您确实喜欢这样:
public void deleteRecord() {
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
Query searchQuery = new Query(Criteria.where("id").is(34562341112313));
mongoOperation.remove(searchQuery, Your_entity_class.class);
logger.info("Delete success");
}
This is my realistic example: 这是我的现实示例:
/**
* Delete by condition(s).
*/
public void deleteJob() {
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
Query searchQuery = new Query(Criteria.where("company").is("DCV"));
mongoOperation.remove(searchQuery, Job.class);
logger.info("Đã xóa các công việc đăng bởi DCV.");
}
Source: https://github.com/SmartJobVN/MongoDB_SpringDataMongo/blob/master/src/main/java/vn/smartJob/jobs/MongoSpringJavaConfigApplication.java#L132 来源: https : //github.com/SmartJobVN/MongoDB_SpringDataMongo/blob/master/src/main/java/vn/smartJob/jobs/MongoSpringJavaConfigApplication.java#L132
Reference: http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/ 参考: http : //docs.spring.io/spring-data/mongodb/docs/current/reference/html/
You should delete it like this: 您应该这样删除它:
@Repository
public class AppDaoClass{
@Autowired
MongoTemplate mongoTemplate;
@Override
public void deleteSomething(String somethingId) {
mongoTemplate.remove(Query.query(Criteria.where("somethingId").is(somethingId)), Ticket.class);
}
}
The first "somethingId" is the name you gave it in your model, and the second somethingId is for the Parametar you are giving in you method . 第一个“ somethingId”是您在模型中为其指定的名称,第二个“ somethingId”是您在method中提供的Parametar的名称 。
And your Domain Model: 和您的域模型:
@Document
public class Model {
@Id
private String somethingId;
private String someName;
private String someOtherName;
}
Be sure to user proper annotations for your classes @Document and @Repository . 确保为类@Document和@Repository使用正确的注释。 And add an @Id annotation to your ID field. 并将@Id注释添加到您的ID字段。
Hope this helps. 希望这可以帮助。
This is the way you can delete records in spring data mongoDB using MongoTemplate 这是使用MongoTemplate删除Spring数据mongoDB中的记录的方法
WriteResult writeResult=mongoTemplate.remove(query,"collection_name");
OR 要么
WriteResult writeResult=mongoTemplate.remove(query,EntityClassName.class);
You can also use repository Pattern 您还可以使用存储库模式
@Document(collection = "user")
public class User {
@Id
private String id;
private String username;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
@Repository
public interface UserRepository extend MongoRepository<User, String>{
public void delete(String id);
public void delete(User user);
public void deleteByUsername(String username);
}
you can use these method anywhere to delete records also u can write your custom methods 您可以在任何地方使用这些方法删除记录,也可以编写自定义方法
@Query(value = "{'_id' : ?0}", delete = true)
void deleteById(String id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.