26

How do i query a table with multiple conditions? Here are the examples both working:

Post.findAll({ where: ['deletedAt IS NULL'] }).success()

and

Post.findAll({ where: {topicId: req.params.id} }).success()

Then, if i need conditions combined, i feel like i need to do something like

Post.findAll({ where: [{topicId: req.params.id}, 'deletedAt IS NULL'] }).success()

, but it doesn't work.
What is the syntax the sequelize waits for?

node debug says:

DEBUG: TypeError: Object # has no method 'replace'

if it matters...

5 Answers 5

29

You can do:

Post.findAll({ where: {deletedAt: null, topicId: req.params.id} })

Which would be translated to deletedAt IS NULL

BTW, if you need deletedAt NOT IS NULL, use:

Post.findAll({ where: {deletedAt: {$ne: null}, topicId: req.params.id} })
Sign up to request clarification or add additional context in comments.

1 Comment

Please update your answer to check multiple where, show are only showing one condition where as he needs to check number and null.
12

Just do:

Post.findAll(
    { where: ["topicId = ? AND deletedAt IS NULL", req.params.id] }
).success()

Or do you need an or query?

3 Comments

Its AND that i needed, and your solution perfectly fits my needs, so thank you.
I must say its kinda obvious what u have done there, how could I not have done so myself?) But it feels more like workaround, I'm still curious what would I do if both conditions had parameters? Or if there'd be more conditions? Sequelize manuals doesn't have anything on such cases...
Nvm, i just found out that there can be several parameters provided subsequently for '?'s. Thanks again)
10

Please note that as of this posting and the 2015 version of sequelize, the above answers are out of date. I am posting the solution that I got working in hopes of saving someone some time.

filters["State"] = {$and: [filters["State"], {$not: this.filterSBM()}] };

Note the $ and array inside the JSON object and lack of ? token replacement.

Or put in a more general way, since that might help someone wrap their head around it:

{ $and: [{"Key1": "Value1"}, {"Key2": "Value2"}] }

2 Comments

Instead of mentioning the most recent version of sequelize, it would be useful if you quote the exact version. When I read your 2015 post in 2020, fairly sure the version you referenced is not the most recent version anymore.
Reasonable. I also wrote that off the cuff while on the job I had 5 years ago, but I'll bear that in mind.
8

This gives me SELECT `id`, `title`, `description`, `test`, `published`, `createdAt`, `updatedAt` FROM `tests` AS `test` WHERE (`test`.`title` LIKE '%3%' OR `test`.`description` LIKE '%2%');

exports.findAll = (req, res) => {
    const title = req.query.title;
    const description = req.query.description;
    Tutorial.findAll({
            where: {
                [Op.or]: [{
                        title: {
                            [Op.like]: `%${title}%`
                        }
                    },
                    {
                        description: {
                            [Op.like]: `%${description}%`
                        }
                    }
                ]
            }
        })
        .then(data => {
            res.send(data);
        })
        .catch(err => {
            res.status(500).send({
                message: err.message || "Some error occurred while retrieving tutorials."
            });
        });

};

1 Comment

This is the right answer. For some reason, most of the people above provided answers for AND condition. This works in Sequelize 6.
7

In new version try this

Post.findAll({
  where: {
    authorId: 12,
    status: 'active'
  }
}).then(function (data) {
    res.status(200).json(data)
            })
   .catch(function (error) {
                res.status(500).json(error)
   });;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.