Condition Builder
Quickly build conditions using MongoPlus
Note:
- The first parameter
boolean condition
indicates whether this condition will be included in the final SQL.
For example:javaquery.like(StringUtils.isNotBlank(name), Entity::getName, name) .eq(age != null && age >= 0, Entity::getAge, age)
- Multiple methods in the code block below will automatically add a
boolean
parameter from top to bottom, defaulting totrue
.
Warning:
Do not transmit QueryChainWrapper
in RPC calls
QueryChainWrapper
is heavy.- Transmitting
QueryChainWrapper
is like using a map in a controller—it’s convenient initially, but painful to maintain. - Correct RPC approach: Use a DTO, then the receiver executes queries based on the DTO.
custom
custom(BasicDBObject basicDBObject);
custom(BSON bson);
- Custom condition
- Example:
new BasicDBObject("$eq", new BasicDBObject("_id","1"))
combine
combine(boolean condition, SFunction<QueryChainWrapper<T,?>, QueryChainWrapper<T,?>> function)
combine(SFunction<QueryChainWrapper<T,?>, QueryChainWrapper<T,?>> function)
combine(QueryChainWrapper<?,?> queryChainWrapper)
combine(boolean condition, QueryChainWrapper<?,?> queryChainWrapper)
- Conditions in
combine
are grouped together, often used foror
/and
.
Example (constructing OR condition):
or(wrapper -> wrapper.eq(User::getUserName, "John").like(User::getUserName, "1"))
Resulting query:
{ "or": [{ "userName": { "eq": "John" } }, { "userName": { "like": "1" } }] }
Using combine
:
or(wrapper ->
wrapper.combine(combineWrapper ->
combineWrapper.eq(User::getUserName, "John").like(User::getUserName, "1")
)
)
Resulting query:
{ "or": [{ "userName": { "eq": "John", "like": "1" } }] }
Note:
- When using
combine
, make sure all field names inside are the same.
eq
eq(SFunction<T,Object> column, Object val)
eq(boolean condition, SFunction<T,Object> column, Object val)
- Equal
=
- Example:
eq("name", "John")
→name = 'John'
ne
ne(SFunction<T,Object> column, Object val)
ne(boolean condition, SFunction<T,Object> column, Object val)
- Not equal
<>
- Example:
ne("name", "John")
→name <> 'John'
gt
gt(SFunction<T,Object> column, Object val)
gt(boolean condition, SFunction<T,Object> column, Object val)
- Greater than
>
- Example:
gt("age", 18)
→age > 18
gte
gte(SFunction<T,Object> column, Object val)
gte(boolean condition, SFunction<T,Object> column, Object val)
- Greater than or equal
>=
- Example:
gte("age", 18)
→age >= 18
lt
lt(SFunction<T,Object> column, Object val)
lt(boolean condition, SFunction<T,Object> column, Object val)
- Less than
<
- Example:
lt("age", 18)
→age < 18
lte
lte(SFunction<T,Object> column, Object val)
lte(boolean condition, SFunction<T,Object> column, Object val)
- Less than or equal
<=
- Example:
lte("age", 18)
→age <= 18
like
like(SFunction<T,Object> column, Object val)
like(boolean condition, SFunction<T,Object> column, Object val)
- LIKE '%value%'
- Example:
like("name", "John")
likeLeft
likeLeft(SFunction<T,Object> column, Object val)
likeLeft(boolean condition, SFunction<T,Object> column, Object val)
- LIKE '%value'
- Example:
likeLeft("name", "^John")
likeRight
likeRight(SFunction<T,Object> column, Object val)
likeRight(boolean condition, SFunction<T,Object> column, Object val)
- LIKE 'value%'
- Example:
likeRight("name", "John$")
in
in(SFunction<T,Object> column, Collection<?> value)
in(boolean condition, SFunction<T,Object> column, Collection<?> value)
- Field IN (value.get(0), value.get(1), ...)
- Example:
in("age", Arrays.asList(1,2,3))
→age IN (1,2,3)
in(SFunction<T,Object> column, Object... values)
in(boolean condition, SFunction<T,Object> column, Object... values)
- Field IN (v0, v1, ...)
- Example:
in("age", 1, 2, 3)
→age IN (1,2,3)
nin
nin(SFunction<T,Object> column, Collection<Object> valueList)
nin(boolean condition, String column, Collection<Object> valueList)
- Field NOT IN (...)
- Example:
nin("age", Arrays.asList(1,2,3))
→age NOT IN (1,2,3)
and
and(LambdaQueryChainWrapper<T> lambdaQueryChainWrapper)
and(boolean condition, LambdaQueryChainWrapper<T> lambdaQueryChainWrapper)
- AND (used inside OR)
orderByAsc
orderByAsc(SFunction<T,Object> columns)
orderByAsc(boolean condition, SFunction<T,Object> columns)
- ORDER BY ... ASC
- Example:
orderByAsc("id", "name")
orderByDesc
orderByDesc(SFunction<T,Object> columns)
orderByDesc(boolean condition, SFunction<T,Object>... columns)
- ORDER BY ... DESC
- Example:
orderByDesc("id", "name")
or
or(SFunction<T,Object> column, Object value)
or(boolean condition, String column, Object value)
or(QueryChainWrapper<?,?> queryChainWrapper)
or(boolean condition, QueryChainWrapper<?,?> queryChainWrapper)
- OR
nor
nor(boolean condition, QueryChainWrapper<?,?> queryChainWrapper)
nor(QueryChainWrapper<?,?> queryChainWrapper)
nor(boolean condition, SFunction<T,Object> column, Object value)
nor(SFunction<T,Object> column, Object value)
nor(boolean condition, String column, Object value)
nor(String column, Object value)
- Documents must not match any of the conditions
type
type(SFunction<T,Object> column, TypeEnum value)
type(String column, TypeEnum value)
type(SFunction<T,Object> column, String value)
type(String column, String value)
type(SFunction<T,Object> column, Integer value)
type(String column, Integer value)
- Specify field type for query
exists
exists(SFunction<T,Object> column, Boolean value)
exists(boolean condition, SFunction<T,Object> column, Boolean value)
- Field exists or not
not
not(CompareCondition compareCondition)
not(boolean condition, CompareCondition compareCondition)
- Negation
expr
expr(CompareCondition compareCondition)
expr(boolean condition, CompareCondition compareCondition)
- Compute expression
mod
mod(boolean condition, SFunction<T,Object> column, long divide, long remain)
mod(SFunction<T,Object> column, long divide, long remain)
mod(boolean condition, SFunction<T,Object> column, Collection<Long> value)
mod(SFunction<T,Object> column, Collection<Long> value)
mod(boolean condition, String column, long divide, long remain)
mod(String column, long divide, long remain)
mod(boolean condition, String column, Collection<Long> value)
mod(String column, Collection<Long> value)
- Field modulo condition
elemMatch
elemMatch(boolean condition, SFunction<T,Object> column, QueryChainWrapper<?,?> queryChainWrapper)
elemMatch(SFunction<T,Object> column, QueryChainWrapper<?,?> queryChainWrapper)
elemMatch(boolean condition, String column, QueryChainWrapper<?,?> queryChainWrapper)
elemMatch(String column, QueryChainWrapper<?,?> queryChainWrapper)
- Match values in an array
between
Children between(boolean condition, SFunction<T,Object> column, Object gte, Object lte, boolean convertGtOrLt)
Children between(SFunction<T,Object> column, Object gte, Object lte, boolean convertGtOrLt)
Children between(boolean condition, String column, Object gte, Object lte, boolean convertGtOrLt)
Children between(String column, Object gte, Object lte, boolean convertGtOrLt)
- Between ... and ...
convertGtOrLt
: whether to convert to >= / <=
all
all(boolean condition, SFunction<T,Object> column, Collection<Object> value)
all(SFunction<T,Object> column, Collection<Object> value)
all(boolean condition, String column, Collection<Object> value)
all(String column, Collection<Object> value)
- Array contains all specified elements
regex
regex(boolean condition, SFunction<T,Object> column, Object value)
regex(SFunction<T,Object> column, Object value)
regex(boolean condition, String column, Object value)
regex(String column, Object value)
- Regex query
text
text(boolean condition, Object value)
text(Object value)
text(boolean condition, Object value, TextSearchOptions textSearchOptions)
text(Object value,TextSearchOptions textSearchOptions)
- 文本查询
size
size(String fieldName, int size);
- Matches all documents where the field value is an array of the specified size.
bitsAllClear
bitsAllClear(SFunction<T,?> fieldName, long bitmask);
- Matches all documents where all bit positions in the field are clear.
bitsAllSet
bitsAllSet(SFunction<T,?> fieldName, long bitmask);
- Matches all documents where all bit positions in the field are set.
bitsAnyClear
bitsAnyClear(SFunction<T,?> fieldName, long bitmask);
- Matches all documents where any bit position in the field is clear.
bitsAnySet
bitsAnySet(SFunction<T,?> fieldName, long bitmask);
- Matches all documents where any bit position in a field is set
QueryWrapper
::: Tip Note: Inherited from AbstractWrapper, its internal property entity is also used to generate where conditions
And LambdaQueryWrapper, which can be obtained through the new QueryWrapper().lambda() method :::
project
project(Projection... projection)
project(List<Projection> projectionList)
select(boolean displayId, Projection... projection)
- Set query fields ::: Tip Note: The above methods are divided into two categories.
The second category is for filtering query fields (excluding primary keys). If the input parameter does not contain class, the entity
property in the wrapper
must have a value before calling it!
For repeated calls of these two types of methods, the last call will prevail.
:::
- Example:
select("id", "name", "age")
- Example:
select(i -> i.getProperty().startsWith("test"))
UpdateWrapper
::: Tip Note: Inherited from AbstractWrapper
, its internal property entity
is also used to generate where conditions and LambdaUpdateWrapper
, and can be obtained via the new UpdateWrapper().lambda()
method! :::
set
set(String column, Object val)
set(boolean condition, String column, Object val)
- SQL SET Field
- Example:
set("name", "老李头")
- Example:
set("name", "")
---> The database field value becomes empty string - Example:
set("name", null)
---> The database field value becomesnull
setOnInsert
setOnInsert(boolean condition, SFunction<T,Object> column, Object value);
setOnInsert(SFunction<T,Object> column, Object value);
setOnInsert(boolean condition, String column, Object value);
setOnInsert(String column, Object value);
- Assigns a specified value to a field in a document.
- Example:
setOnInsert(User::getUserName,"张三")
push
push(boolean condition,SFunction<T,Object> column,Object value);
push(SFunction<T,Object> column,Object value);
push(boolean condition,String column,Object value);
push(String column,Object value);
push(boolean condition,SFunction<T,Object> column,Object ... value);
push(SFunction<T,Object> column,Object ... value);
push(boolean condition,String column,Object ... value);
push(String column,Object ... value);
push(boolean condition, SFunction<T,Object> column, List<?> value);
push(SFunction<T,Object> column, List<?> value);
push(boolean condition, String column, List<?> value);
push(String column, List<?> value);
- Push the specified value into the array
- Example:
push(User::getRoleList,new ArrayList<>())
inc
inc(boolean condition,SFunction<T,Object> column,Number value);
inc(SFunction<T,Object> column,Number value);
inc(boolean condition,String column,Number value);
inc(String column,Number value);
- Atomic increment of the specified value
- Example:
inc(User::getAge,1)
currentDate
currentDate(boolean condition,SFunction<T,Object> column);
currentDate(SFunction<T,Object> column);
currentDate(boolean condition,String column);
currentDate(String column);
currentDate(boolean condition,SFunction<T,Object> column, CurrentDateType currentDateType);
currentDate(SFunction<T,Object> column, CurrentDateType currentDateType);
currentDate(boolean condition,String column, CurrentDateType currentDateType);
currentDate(String column, CurrentDateType currentDateType);
- Sets the value of a field to the current date or timestamp.
- Example:
currentDate(User::getCreateTime,CurrentDateType.DATE)
min
min(boolean condition,SFunction<T,Object> column, Object value);
min(SFunction<T,Object> column, Object value);
min(boolean condition,String column, Object value);
min(String column, Object value);
- Updates a specified field if its value is less than the input value.
- Example:
min(User::getSalt,100)
- Example:
min(User::getCreateTime,LocalDateTime.now())
max
max(boolean condition,SFunction<T,Object> column, Object value);
max(SFunction<T,Object> column, Object value);
max(boolean condition,String column, Object value);
min(String column, Object value);
- Update if the specified field value is greater than the input value
- Example:
max(User::getSalt,100)
- Example:
max(User::getCreateTime,LocalDateTime.now())
mul
mul(boolean condition,SFunction<T,Object> column,Number value);
mul(SFunction<T,Object> column,Number value);
mul(boolean condition,String column,Number value);
mul(String column,Number value);
- Multiplies the value of the specified field by value.
- Example:
mul(User::getAge,21)
rename
rename(boolean condition, String oldFieldName,String newFieldName);
rename(String oldFieldName,String newFieldName);
rename(boolean condition, SFunction<O,Object> oldFieldName,String newFieldName);
rename(SFunction<O,Object> oldFieldName,String newFieldName);
rename(boolean condition, SFunction<O,Object> oldFieldName,SFunction<N,Object> newFieldName);
rename(SFunction<O,Object> oldFieldName,SFunction<N,Object> newFieldName);
- Update field names
- Example:
rename(User::getUserName,"user_name")
- Example:
rename(User::getUserName,User::getUserName)
unset
unset(SFunction<T,Object>... columns);
unset(boolean condition,SFunction<T,Object>... columns);
unset(String... columns);
unset(boolean condition,String... columns);
unset(List<String> columns);
unset(boolean condition,List<String> columns);
- Delete specific fields
- Example:
unset(User::getUserName,User::getAge)
addToSet
addToSet(boolean condition,SFunction<T,Object> column,Object value,boolean each);
addToSet(SFunction<T,Object> column,Object value,boolean each);
addToSet(boolean condition,String column,Object value,boolean each);
addToSet(String column,Object value,boolean each);
- Add values to array
- Example:
addToSet(User::getRoleList,new ArrayList<>(),true)
pop
pop(boolean condition,SFunction<T,Object> column, PopType popType);
pop(SFunction<T,Object> column, PopType popType);
pop(boolean condition,String column, PopType popType);
pop(String column, PopType popType);
- Delete the first or last element in the array
- Example:
pop(User::getRoleList,PopType.FIRST)
pull
pull(boolean condition,SFunction<T,Object> column,Object value);
pull(SFunction<T,Object> column,Object value);
pull(boolean condition,QueryChainWrapper<?,?> wrapper);
pull(boolean condition,SFunction<QueryChainWrapper<?,?>,QueryChainWrapper<?,?>> function);
pull(QueryChainWrapper<?,?> wrapper);
pull(SFunction<QueryChainWrapper<?,?>,QueryChainWrapper<?,?>> function);
pull(boolean condition,String column,Object value);
pull(String column,Object value);
- Delete instances in the array that meet the condition or match the specified value
- Example:
pull(new QueryWrapper<User>().eq(User::getUserName,"Zhang San"))
pullAll
pullAll(boolean condition,SFunction<T,Object> column, Collection<?> values);
pullAll(SFunction<T,Object> column, Collection<?> values);
pullAll(boolean condition,SFunction<T,Object> column, Object... values);
pullAll(SFunction<T,Object> column, Object... values);
pullAll(boolean condition,String column, Collection<?> values);
pullAll(String column, Collection<?> values);
pullAll(boolean condition,String column, Object... values);
pullAll(String column, Object... values);
pullAll(boolean condition,MutablePair<String, Collection<?>>... pullAllPair);
pullAll(MutablePair<String, Collection<?>>... pullAllPair);
- Delete instances from an array that meet a condition or a specified value.
- Example:
pullAll(User::getId, Arrays.asList(1,2,3))
- Example:
pullAll(MutablePair.of(User::getId, Arrays.asList(1,2,3)))
lambda
- Get
LambdaWrapper
In QueryWrapper
, get LambdaQueryWrapper
. In UpdateWrapper , you obtain LambdaUpdateWrapper .
Chaining Lambda Calls
// Differentiation:
// Chaining Normal Calls
UpdateChainWrapper<T> update();
// Chaining Lambda Calls. Note: Kotlin is not supported.
LambdaUpdateChainWrapper<T> lambdaUpdate();
// Equivalent Example:
query().eq("id", value).one();
lambdaQuery().eq(Entity::getId, value).one();
// Equivalent Example:
update().eq("id", value).remove();
lambdaUpdate().eq(Entity::getId, value).remove();