Skip to content

3.4 Filtering

Lucas Rocha edited this page Feb 8, 2019 · 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 usar 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 usar 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 usar 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 usar wants results that age is greater than 30.

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

Result:

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

Example: Search where the usar 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 usar wants results that age is lower than 30

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

Result:

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

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

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

Result:

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

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.


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

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' }}] 
}
Clone this wiki locally