Skip to content

3.4 Filtering

Jefferson Medeiros edited this page Aug 5, 2021 · 3 revisions

1. Simple Filtering

Example: Search where the user wants results that name equals elvis and age equals 80.

http://localhost:3000/?name=elvis&age=80

Result:

filters: { 
   "name": "elvis",
   "age": 80
}

2. Sub-Level Filtering

Example: Search where the user wants results that name equals elvis and city address equals New York.

http://localhost:3000/?name=elvis&address.city=New%20York

Result:

filters: { 
   "name": "elvis",
   "address.city": "New York"
}

3. Filtering With AND Operation

Example: Search where the user wants results that name equals elvis and john.

http://localhost:3000/?name=elvis&name=john

Result:

filters: { 
    $and: [
        { name: 'elvis' },
        { name: john } 
    ]}
}

4. Filtering With OR Operation

Example: Search where the user wants results that name equals elvis or john.

http://localhost:3000/?name=elvis,john

Result:

filters: { 
    $or: [
        { name: 'elvis' },
        { name: john } 
    ]}
}

5. Filtering With Logic Operators

Example: Search where the user wants results that age is greater than 30.

http://localhost:3000/?age=gt:30

Result:

filters: { 
    age: { $gt: 30 } 
} 

Example: Search where the user wants results that age is greater or equal than 30

http://localhost:3000/?age=gte:30

Result:

filters: { 
    age: { $gte: 30 } 
} 

Example: Search where the user wants results that age is lower than 30

http://localhost:3000/?age=lt:30

Result:

filters: { 
    age: { $lt: 30 } 
} 

Example: Search where the user wants results that age is lower or equal than 30

http://localhost:3000/?age=lte:30

Result:

filters: { 
    age: { $lte: 30 } 
} 

Example: Search where the user wants results that type is different than ‘admin‘.

http://localhost:3000/?type=ne:admin

Result:

filters: { 
    type: { $ne: 'admin' } 
} 

6. Filtering With Search

Example: Search where the user want results that the name starts with value ‘elvis’.

http://localhost:3000/?name=elvis*	

Result:

filters: { 
    name: { 
        '$regex': '^elvis' 
        '$options': 'i',
    }
}

Example: Search where the user want results that the name ends with the value ‘elvis’.

http://localhost:3000/?name=*elvis	

Result:

filters: { 
    name: { 
        '$regex': 'elvis&' 
        '$options': 'i',
    }
}

Example: Search where the user wants results for name that contains the value ‘elvis’ in any position.

http://localhost:3000/?name=*elvis*	

Result:

filters: { 
    name: { 
        '$regex': 'elvis' 
        '$options': 'i',
    }
}

7. Filtering With Date

The name of parameter used in date filtering depends of the date_fields configuration used when instantiating the middleware. In the examples below, the default settings will be used, and the name of the date parameter will be created_at.

Note: The supported date format is yyyy-MM-dd (commom date) or yyyy-MM-ddThh:mm:ss (datetime). If the start_at parameter is passed in commom date format, it will normalized as datetime format yyyy-MM-ddT00:00:00. If the end_at paramter is passed in commom date format, it will normalized as datetime format yyyy-MM-ddT23:59:59.


Example: Search where the user wants results between 2018-12-10 and 2018-11-10.

http://localhost:3000/?start_at=2018-11-10&end_at=2018-12-10

Result:

filters: {
    $and: [ 
        { created_at: { $lt: '2018-12-10T23:59:59' }}, 
        { created_at: { $gte: '2018-11-10T00:00:00' }}] 
}

Example: Search where the user wants results between 2018-12-10 and the current date. In this example, the current day is: 2019-02-08

http://localhost:3000/?start_at=2018-12-10&end_at=today

Result:

filters: {
    $and: [ 
        { created_at: { $lt: '2019-02-08T23:59:59' }}, 
        { created_at: { $gte: '2018-12-10T00:00:00' }}] 
}

Example: Search where the user wants results from 10 days before 2018-12-11.

http://localhost:3000/?end_at=2018-12-11&period=10d

Result:

filters: {
    $and: [ 
        { created_at: { $lt: '2018-12-11T23:59:59' }}, 
        { created_at: { $gte: '2018-12-01T00:00:00' }}] 
}

Example: Search where the user wants results from 10 days after 2018-12-01.

http://localhost:3000/?start_at=2018-12-01&period=10d

Result:

filters: {
    $and: [ 
        { created_at: { $lt: '2018-12-11T23:59:59' }}, 
        { created_at: { $gte: '2018-12-01T00:00:00' }}] 
}

Example: Search where the user wants results from 2 weeks before 2018-12-26.

http://localhost:3000/?end_at=2018-12-26&period=2w

Result:

filters: {
    $and: [ 
        { created_at: { $lt: '2018-12-26T23:59:59' }}, 
        { created_at: { $gte: '2018-12-12T00:00:00' }}] 
}

Example: Search where the user wants results from 2 weeks after 2018-12-12.

http://localhost:3000/?start_at=2018-12-12&period=2w

Result:

filters: {
    $and: [ 
        { created_at: { $lt: '2018-12-26T23:59:59' }}, 
        { created_at: { $gte: '2018-12-12T00:00:00' }}] 
}

Example: Search where the user wants results from 1 month before 2019-01-24.

http://localhost:3000/?end_at=2019-01-24&period=1m

Result:

filters: {
    $and: [ 
        { created_at: { $lt: '2019-01-24T00:59:59' }}, 
        { created_at: { $gte: '2018-12-24T00:00:00' }}] 
}

Example: Search where the user wants results from 1 month after 2018-12-24.

http://localhost:3000/?start_at=2018-12-24&period=1m

Result:

filters: {
    $and: [ 
        { created_at: { $lt: '2019-01-24T00:59:59' }}, 
        { created_at: { $gte: '2018-12-24T00:00:00' }}] 
}

Example: Search where the user wants results from 1 year before 2018-12-24.

http://localhost:3000/?end_at=2018-12-24&period=1y

Result:

filters: {
    $and: [ 
        { created_at: { $lt: '2018-12-24T00:59:59' }}, 
        { created_at: { $gte: '2017-12-24T00:00:00' }}] 
}

Example: Search where the user wants results from 1 year after 2017-12-24.

http://localhost:3000/?start_at=2017-12-24&period=1y

Result:

filters: {
    $and: [ 
        { created_at: { $lt: '2018-12-24T00:59:59' }}, 
        { created_at: { $gte: '2017-12-24T00:00:00' }}] 
}