Skip to main content
Version: 1.6.x

Q Expressions

Q Expressions (Query Expressions) are used to construct query conditions for data models. They provide a structured way to express complex query logic, supporting various operators and logical combinations.

Basic Rules​

The basic construction rules for Q Expressions are as follows:

  • Basic Format: Q(<fieldId>, <operator>, <value>)

    • fieldId: Field identifier (string), supports related fields (e.g., customer__custName)
    • operator: Operator (string)
    • value: Comparison value
  • Note: When passing a single condition query as a parameter, it must be wrapped in an outer Q().

    • Example: Q(Q('age', '>', 18))
  • Logical Connection: Use Q.AND and Q.OR to connect multiple Q expressions.

    • Format: Q(Q1, <Logic>, Q2)

Operator Details​

Comparison Operators​

OperatorDescriptionExample
=Equal toQ('age', '=', 18)
!=, neNot equal toQ('age', '!=', 18)
>, gtGreater thanQ('age', '>', 18)
>=, gteGreater than or equal toQ('age', '>=', 18)
<, ltLess thanQ('age', '<', 18)
<=, lteLess than or equal toQ('age', '<=', 18)

Inclusion Operators​

OperatorDescriptionExample
inIn listQ('status', 'in', ['active', 'pending'])
ninNot in listQ('status', 'nin', ['deleted'])

Fuzzy Matching​

OperatorDescriptionExample
likeContainsQ('name', 'like', 'John')
nlikeDoes not containQ('name', 'nlike', 'Test')
startswithStarts withQ('code', 'startswith', 'A')
endswithEnds withQ('code', 'endswith', 'X')
likeanyContains anyQ('tags', 'likeany', ['A', 'B'])
nlikeanyDoes not contain anyQ('tags', 'nlikeany', ['X', 'Y'])

Null Value Check​

OperatorDescriptionValue ExplanationExample
isnullIs null1 (Null), 0 (Not Null)Q('remark', 'isnull', 1)

Range Query​

OperatorDescriptionExample
rangeRange intervalQ('age', 'range', [18, 30])

Special Operators​

OperatorDescriptionExample/Format
belongBelonging relationshipQ('address', 'belong', {"province": "Beijing"})
nbelongNon-belonging relationshipQ('address', 'nbelong', {"province": "Shanghai"})
yearYear matchingQ('createTime', 'year', 2023)
monthMonth matchingQ('createTime', 'month', 10)
weekWeek matchingQ('createTime', 'week', 40)
dayDay matchingQ('createTime', 'day', 1)
provinceProvince matchingQ('address', 'province', 'Guangdong')
cityCity matchingQ('address', 'city', 'Shenzhen')
districtDistrict matchingQ('address', 'district', 'Nanshan District')

Examples​

Basic Queries​

// Age greater than 18
Q('age', 'gt', 18)

// Status is in the list
Q('status', 'in', ['active', 'pending'])

// Fuzzy match name
Q('name', 'like', 'John')

// Check if field is null
Q('remark', 'isnull', 1)

Supports accessing fields of related objects via the __ symbol.

// Query records where the related customer's name is John
Q('customer__custName', '=', 'John')

Complex Object Queries​

// Address belonging query
Q('address', 'belong', {
"province": "Beijing",
"city": "",
"district": ""
})

Combined Condition Queries​

Use Q.AND and Q.OR for logical combinations.

// AND combination: Age > 18 AND Age < 30
Q(Q('age', 'gt', 18), Q.AND, Q('age', 'lt', 30))

// Complex combination: (Age > 18 AND Age < 30) OR Is VIP
Q(
Q(Q('age', 'gt', 18), Q.AND, Q('age', 'lt', 30)),
Q.OR,
Q('vip', '=', 1)
)

Practical Usage Examples​

Q expressions are typically passed as parameters to data model methods for filtering data.

javascript
// Basic condition query (Note: Single condition must be wrapped in Q())
const users = await this.app.models.UserModel.query(
Q(Q('status', '=', 'active')),
null,
null,
1,
20
);

// Complex combined condition query
const result = this.app.models.await OrderModel.query(
Q(Q('amount', 'gt', 100), Q.AND, Q('createTime', 'year', 2023)),
null,
null,
1,
20
);
python
# Basic condition query (Note: Single condition must be wrapped in Q())
app.models.UserModel.query(Q(Q('status', '=', 'active')), None, None, 1, 20)

# Complex combined condition query
app.models.OrderModel.query(
Q(Q('amount', 'gt', 100), Q.AND, Q('createTime', 'year', 2023)),
None,
None,
1,
20
)