Spring Data 和 MongoDB 存储库 - 如何创建更新查询? [英] Spring Data and MongoDB repository - how to create an update query?
本文介绍了Spring Data 和 MongoDB 存储库 - 如何创建更新查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下 jpa 存储库:
I have the following jpa repository:
@Query("UPDATE PlayerAccount pa SET pa.password = ?3 WHERE pa.id = ?1 AND pa.password = ?2")
@Modifying
public int updatePasswordWithValidation(Long playerAccountId, String oldPasswordDB, String encodePassword);
现在,我想为 mongoDB 存储库实现类似的更新查询:
Now, i would like to implement a similar update query for a mongoDB repository:
@Query("update( { _id: ObjectId(' $1 ') }, { $set: { messageStatus: $2} })")
但它不起作用.任何关于自定义 mongo 存储库更新的参考资料?
But it doesn't work. any references to how a customized mongo repository update looks like?
谢谢
推荐答案
MongoDB 查询语言是一种仅查询语言.因此,没有更新查询之类的东西.如果您需要使用 MongoDB 之上的 Spring Data 存储库执行专用更新,则需要自定义实现方法.
The MongoDB query language is a query-only language. Thus, there's no such thing as an update query. If you need to executed dedicated updates with a Spring Data repository on top of MongoDB, you need a custom implementation method.
// Interface for custom functionality
interface SomeCustomRepository {
void updateMethod(…);
}
// Custom implementation
class FooRepositoryImpl implements SomeCustomRepository {
public void updateMethod(…) {
mongoTemplate.update(…);
}
}
// Core repository declaration combining CRUD functionality and custom stuff
interface FooRepository extends CrudRepository<Foo, ObjectId>, SomeCustomRepository {
…
}
这种方法也在 参考文档.
这篇关于Spring Data 和 MongoDB 存储库 - 如何创建更新查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文