Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Filter model data in LoopBack

    Bram BorggreveBram Borggreve

    In this lesson we will look at the filters that can be used to retrieve and manipulate the data stored in LoopBack.

    We will use Postman to interact with the /Categories and /Products endpoints.

    Using the URL parameters we will apply at the various filters LoopBack has to offer: include, order, where, fields, limit and offset.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    We can use filters to manipulate which data gets returned by loopback. Let's see this in action. We'll interact with the GetCategories and GetProducts endpoints. I used the API Explorer to create some sample data to play with.

    We can apply the filters using URL parameters. For example, with the include filter, we can include the products that belong in a category.

    Now, let's get to our products endpoints and we include the category in the result. We use the order filter to sort in price ascending or descending. We add a where filter to only return all products with a price higher than 200. Using the where filter, we can also do a wildcard search using like.

    The filter field is used to only show certain fields. When filtering on fields, we see that our category is no longer included in the results. To fix our include filter, we need to make sure that we add our foreign key in the fields filter. Once we add category ID to our fields filter, we see that the category is included again.

    Next, we add a limit filter to limit the number of items in our result. We can use the offset filter to skip items from the results. The last two filters can be useful if you want to page through the data.