Filter Records - Service Factory Reference

Many of the service factory's generated methods allow passing filters to perform an operation, such as to update or delete records matching the filters.

This guide provides examples of using filters.

NoteThe list method is used in the example snippets of this reference, but you can use the same filtering mechanism with any method that accepts filters.

Match Exact Value#

Code
1const posts = await postModuleService.listPosts({2  title: "My Post 2",3})

If you pass a property with its value, only records whose properties exactly match the value are selected.

In the example above, only posts having the title My Post 2 are retrieved.


Doesn't Match Exact Value#

Code
1const posts = await postModuleService.listPosts({2  title: {3    $ne: "My Post",4  },5})

To find records with a property that doesn't match a value, pass an object with a $ne property. Its value is the value that a record's property shouldn't match.

In the example above, only posts that don't have the title My Post are retrieved.


Match Multiple Values#

Code
1const posts = await postModuleService.listPosts({2  views: [3    50,4    100,5  ],6})

To find records with a property matching multiple values, pass an array of those values as the property's value in the filter.

In the example above, only posts having either 50 or 100 views are retrieved.


Don't Match Multiple Values#

Code
1const posts = await postModuleService.listPosts({2  title: {3    $nin: [4      "My Post",5    ],6  },7})

To find records with a property that doesn't match one or more values, pass an object with a $nin property. Its value is an array of multiple values that a record's property shouldn't match.

In the example above, only posts that don't have the title My Post are retrieved.


Match Text Like Value#

NoteThis filter only applies to text-like properties, including text, id, and enum properties.
Code
1const posts = await postModuleService.listPosts({2  title: {3    $like: "My%",4  },5})

To perform a like filter on a record's property, set the property's value to an object with a $like property. Its value is the string to use when applying the like filter.

The example above matches all posts whose title starts with My.


Filter by Null or Not Null#

To retrieve records with a property that is null, set the property's value to null.

For example:

Code
1const posts = await postModuleService.listPosts({2  published_at: null,3})

In the example above, only posts that have a null publish date are retrieved.

On the other hand, to retrieve records with a property that isn't null, set the property's value to an object with a $ne property.

For example:

Code
1const posts = await postModuleService.listPosts({2  published_at: {3    $ne: null,4  },5})

In the example above, only posts that have a publish date are retrieved.


Apply Range Filters#

NoteThis filter only applies to the number and dateTime properties.
Code
1const posts = await postModuleService.listPosts({2  published_at: {3    $lt: new Date(),4  },5})

To filter a record's property to be within a range, set the property's value to an object with any of the following properties:

  1. $lt: The property's value must be less than the supplied value.
  2. $lte: The property's value must be less than or equal to the supplied value.
  3. $gt: The property's value must be greater than the supplied value.
  4. $gte: The property's value must be greater than or equal the supplied value.

In the example above, only posts whose published_at property is before the current date and time are retrieved.

Example: Retrieve Posts Published Today#

Code
1const startToday = new Date()2startToday.setHours(0, 0, 0, 0)3
4const endToday = new Date()5endToday.setHours(23, 59, 59, 59)6
7const posts = await postModuleService.listPosts({8  published_at: {9    $gte: startToday,10    $lte: endToday,11  },12})

The dateTime property also stores the time. So, when matching for an exact day, you must set a range filter to be between the beginning and end of the day.

In this example, you retrieve the current date twice: once to set its time to 00:00:00, and another to set its time 23:59:59. Then, you retrieve posts whose published_at property is between 00:00:00 and 23:59:59 of today.

Example: Range Filter on Number Property#

Code
1const posts = await postModuleService.listPosts({2  views: {3    $gte: 50,4    $lte: 100,5  },6})

In the example above, only posts with views between 50 and 100 are retrieved.


Apply Or Condition#

Code
1const posts = await postModuleService.listPosts({2  $or: [3    {4      title: "My Post",5    },6    {7      published_at: {8        $lt: new Date(),9      },10    },11  ],12})

To use an or condition, pass to the filter object the $or property, whose value is an array of filters.

In the example above, posts whose title is My Post or their published_at date is less than the current date and time are retrieved.


Apply And Condition#

Code
1const posts = await postModuleService.listPosts({2  $and: [3    {4      title: "My Post",5    },6    {7      published_at: {8        $lt: new Date(),9      },10    },11  ],12})

To use an and condition, pass to the filter object the $and property, whose value is an array of filters.

In the example above, only posts whose title is My Post and their published_at date is less than the current date and time are retrieved.


Complex Filters Example#

Code
1const posts = await postModuleService.listPosts({2  $or: [3    {4      $and: [5        { views: { $gte: 50 } },6        { 7          published_at: {8            $gte: new Date(new Date().getFullYear(), 0, 1),9          },10        },11      ],12    },13    {14      title: {15        $like: "%Featured%",16      },17    },18  ],19})

In the example above, posts are retrieved if they meet either of the following conditions:

  1. The post has at least 50 views and was published after the beginning of the current year.
  2. The post's title contains the word Featured.

By combining and and or conditions, you can create complex filters to retrieve records that meet specific criteria.

Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break