Navigate back to the homepage

Sequelize Operators Example with query

Infinitbility
Node, Sequelize
July 2nd, 2021 · 2 min read
Sequelize Operators Example with query

Hello Friends,

Welcome To Infinitbility!

This article based on sequelize operators, here we will learn about How many types of basics sequelize operators and how we can use in query.

We use greater than, less than, Greater than or equal, Less than or equal, Not equal, Equal , sign in sql but in sequelize we use as Operators to done this types of query. One more thing sequelize Operators also use for and and or types of query.

Sequelize more then this operators but we will cover in specfic post for those now we will cover only basics Operators.

Let’s start today article Sequelize Operators Example with query

Table of content

  1. Equal Operator
  2. Not equal Operator
  3. Greater than Operator
  4. Less than Operator
  5. Greater than or equal Operator
  6. Less than or equal Operator
  7. And Operator
  8. Or Operator

For using operator We have to first import Op form Sequelize like below.

1const { Op } = require("sequelize");

Equal Operator ( = )

The Equal Operator use for check same value available in table or not in sql we use equal to sign ( = ) but in sequelize we use Op.eq in query like below example.

1Pug.findAll({
2 where: {
3 age: {
4 [Op.eq]: 7
5 }
6 }
7})

Not equal Operator ( != )

The Not equal Operator use for filter data those value available in your table. In sql we use not equal to sign ( != ) but in sequelize we use Op.ne in query like below example.

1Pug.findAll({
2 where: {
3 age: {
4 [Op.ne]: 7
5 }
6 }
7})

Greater than Operator ( > )

The Greater than Operator use get those data have more value compare to search value in sql we us greater than sign ( > ) for this but in sequelize we use Op.gt like below example.

1Pug.findAll({
2 where: {
3 age: {
4 [Op.gt]: 7
5 }
6 }
7})

Less than Operator ( < )

The Less than Operator use get those data have less value compare to search value in sql we us Less than sign ( < ) for this but in sequelize we use Op.lt like below example.

1Pug.findAll({
2 where: {
3 age: {
4 [Op.lt]: 7
5 }
6 }
7})

Greater than or equal Operator ( >= )

The Greater than or equal Operator same like Greater than Operator but also check same value or not.

In sequelize we use Op.gte to get same or greater value in search.

1Pug.findAll({
2 where: {
3 age: {
4 [Op.gte]: 7
5 }
6 }
7})

Less than or equal Operator ( <= )

The Less than or equal Operator same like Less than Operator but also check same value or not.

In sequelize we use Op.lte to get same or less value in search.

1Pug.findAll({
2 where: {
3 age: {
4 [Op.lte]: 7
5 }
6 }
7})

And Operator ( And )

We use And Operator in sql simple like and keyword but in sequelize we have to use Op.and example like below.

1Pug.findAll({
2 where: {
3 [Op.and]: {
4 age: 8
5 },
6 [Op.and]: {
7 is_active: 8
8 }
9 }
10})

It’s just example to use and Operator don’t go with query optimization.

Or Operator ( or )

We use Or Operator in sql simple like or keyword but in sequelize we have to use Op.or example like below.

1Pug.findAll({
2 where: {
3 [Op.or]: {
4 age: 8
5 },
6 [Op.or]: {
7 is_active: 8
8 }
9 }
10})

Subscribe Our Newsletter You will get monthly free programming book

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Subscribe Our Newsletter You will get monthly free programming book

Tutorials

React Native

Categories

More articles from Infinitbility

Node sequelize update query example

Node sequelize update query example

Sequelize update

July 1st, 2021 · 1 min read
Node sequelize where or query example

Node sequelize where or query example

How to use where or in node sequelize

June 30th, 2021 · 1 min read
© 2020–2021 Infinitbility
Disclaimer
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]