node.js sequelize:多个“where”查询条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10807765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:46:36  来源:igfitidea点击:

node.js sequelize: multiple 'where' query conditions

node.jssequelize.js

提问by Masquer

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?

,但它不起作用。
sequelize 等待的语法是什么?

node debug says:

节点调试说:

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

调试:类型错误:对象 # 没有方法“替换”

if it matters...

如果重要...

采纳答案by JR Smith

Please note that as of this posting and the most recent 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.

请注意,截至本帖和最新版本的 sequelize,上述答案已过时。我正在发布我正在工作的解决方案,希望能节省一些时间。

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

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

请注意 JSON 对象中的 $ 和数组以及缺少 ? 代币替换。

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

或者用更一般的方式,因为这可能会帮助人们理解它:

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

回答by gwae

You can do:

你可以做:

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

Which would be translated to deletedAt IS NULL

这将被翻译成 deletedAt IS NULL

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

顺便说一句,如果您需要deletedAt NOT IS NULL,请使用:

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

回答by sdepold

Just do:

做就是了:

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

Or do you need an or query?

或者你需要一个或查询?

回答by Adiii

In new versiontry this

新版本中试试这个

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

回答by Boris

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%');

这给了我 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."
            });
        });

};