Skip to main content
Version: 1.0.4

Where

Where Filter

The where filter allows you to query and filter data in your API requests using MongoDB-like syntax. This filter can be used to create complex queries with various conditions. Below is a detailed explanation of how to use the where filter in your API requests.

Basic Usage

The where filter is passed as a JSON object, where each key represents a field in your database, and the value represents the condition for that field.

{
"where": {
"field1": "value",
"field2": {
"$operator": "condition"
}
}
}

Supported Operators

The following operators are supported:

  • $eq: Checks if the field is equal to the specified value.
  • $ne: Checks if the field is not equal to the specified value.
  • $gt: Checks if the field is greater than the specified value.
  • $gte: Checks if the field is greater than or equal to the specified value.
  • $lt: Checks if the field is less than the specified value.
  • $lte: Checks if the field is less than or equal to the specified value.
  • $in: Checks if the field's value is in the specified array of values.
  • $nin: Checks if the field's value is not in the specified array of values.
  • $regex: Allows for pattern matching using regular expressions.

Examples

Equality

{
"where": {
"status": "active"
}
}

This will return all records where the status field is equal to "active".

Greater Than

{
"where": {
"age": {
"$gt": 18
}
}
}

This will return all records where the age field is greater than 18.

Regex

{
"where": {
"username": {
"$regex": "^admin"
}
}
}

This will return all records where the username field starts with "admin".

Multiple Conditions

{
"where": {
"age": {
"$gte": 18,
"$lt": 65
},
"status": "active"
}
}

This will return all records where the age field is between 18 and 64 (inclusive) and the status field is equal to "active".

Combining Conditions

You can combine multiple conditions using logical operators such as $and, $or, and $not.

$and Operator

{
"where": {
"$and": [
{ "status": "active" },
{ "age": { "$gte": 18 } }
]
}
}

This will return all records where the status field is "active" and the age field is greater than or equal to 18.

$or Operator

{
"where": {
"$or": [
{ "status": "active" },
{ "age": { "$lt": 18 } }
]
}
}

This will return all records where the status field is "active" or the age field is less than 18.

$not Operator

{
"where": {
"status": {
"$not": {
"$eq": "inactive"
}
}
}
}

This will return all records where the status field is not equal to "inactive".

Notes

  • The where filter is case-sensitive by default.
  • Regular expressions used in the $regex operator follow MongoDB's syntax and options.

Conclusion

The where filter provides a flexible and powerful way to query your data. By leveraging MongoDB-like operators, you can easily create complex queries to retrieve the exact data you need.