如何在mongo spring boot中默认为所有查询添加默认条件 [英] How to add default criteria to all the queries by default in mongo spring boot
问题描述
我需要软删除给定 MongoDB 集合中的文档.为此,我使用了一个名为已删除的布尔值.所以现在当我从数据库中检索数据时,我必须总是提到在 deleted=false
的地方取数据.
I have a requirement to soft delete documents in a given MongoDB collection. For that, I use a boolean called deleted. So now when I am retrieving data from the database, I have to always mention taking the data where the deleted=false
.
例如:
public Organization findOrgById(String id) {
Query query = new Query();
Criteria criteria = Criteria.where(Constants.ENTITY_ID).is(id)
.and(Constants.DELETED).is(false);
query.addCriteria(criteria);
Organization res = mongoTemplate.findOne(query, Organization.class);
return res;
}
有没有办法指定总是默认添加 deleted=false
的所有条件而不在代码本身中提及它?在 Hibernate 核心中有一个注释 @Where
但它不适用于 mongo 文档.
Is there a way to specify that always all the criteria to add deleted=false
by default without mentioning it in the code itself?
In Hibernate core there is an annotation @Where
but it is not working with mongo documents.
推荐答案
我认为最好的方法是扩展 MongoTemplate
类,该类将添加您的 deleted=false
所有 Find 查询的条件.
I think the best way to do this is extend the MongoTemplate
class that will add your deleted=false
condition to all Find queries.
以下是如何使用一种用于执行 findOne
查询的方法的示例:
Here is an example of how to do it with one method used to execute findOne
queries:
public class ExtendedMongoTemplate extends MongoTemplate {
private static final Document DELETED_CRITERIA_DOC = Criteria.where(Constants.DELETED).is(false)
.getCriteriaObject();
@Override
protected <T> T doFindOne(
String collectionName,
Document query,
Document fields,
CursorPreparer preparer,
Class<T> entityClass) {
query.putAll(DELETED_CRITERIA_DOC);
return super.doFindOne(collectionName, query, fields, preparer, entityClass);
}
...
}
该方法在方法doFindOne(Query query, Class> entityClass)
(最后一个委托执行
This method is called in the method doFindOne(Query query, Class<?> entityClass)
(the last one delegates executing
其他要覆盖的方法是:
protected <S, T> List<T> doFind(String collectionName, Document query, Document fields,
Class<S> entityClass, CursorPreparer preparer);
protected <T> T doFindAndRemove(String collectionName, Document query, Document fields,
Document sort, @Nullable Collation collation, Class<T> entityClass);
protected <T> T doFindAndModify(String collectionName, Document query, Document fields, Document sort,
Class<T> entityClass, UpdateDefinition update, @Nullable FindAndModifyOptions options);
protected <T> T doFindAndReplace(String collectionName, Document mappedQuery, Document mappedFields,
Document mappedSort, com.mongodb.client.model.Collation collation, Class<?> entityType,
Document replacement, FindAndReplaceOptions options, Class<T> resultType);
这些方法在低级别执行查询,因此它们接受带有查询条件的 BSON 文档,而不是 Spring 的条件.如果您这样做,Find-methods 将为您的所有查询添加一个额外的条件.
These methods execute queries at low-level, so they accept BSON-documents with the query criteria, not Spring's criteria. If you do this, the Find-methods will add an additional criteria to all you queries.
您也可以以类似的方式覆盖方法 findOne
、find
、findAndModify
等,但是这些方法有很多都使用 doFind*
方法.因此覆盖 doFind*
将导致与所有 Find-queries 一起工作.并且不要忘记也覆盖 findById
(它也在内部使用 doFindOne
).
You also can override methods findOne
, find
, findAndModify
and so on in a similar manner, but there are a lot of these methods that all use doFind*
methods. Thus overriding doFind*
will lead to work with all Find-queries. And don't forget override also findById
(it also uses doFindOne
internally).
顺便说一下,@Where
注解来自Hibernate,但Spring Data Mongo并没有使用它们.它需要自己的注释才能与您的实体一起使用.
By the way, @Where
annotation is from Hibernate, but Spring Data Mongo doesn't use them. It requires its own annotations to work with your entities.
这篇关于如何在mongo spring boot中默认为所有查询添加默认条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!