Mas*_*uer 17 node.js sequelize.js
如何查询具有多个条件的表?以下是两个工作的例子:
Post.findAll({ where: ['deletedAt IS NULL'] }).success()
Run Code Online (Sandbox Code Playgroud)
和
Post.findAll({ where: {topicId: req.params.id} }).success()
Run Code Online (Sandbox Code Playgroud)
然后,如果我需要结合条件,我觉得我需要做类似的事情
Post.findAll({ where: [{topicId: req.params.id}, 'deletedAt IS NULL'] }).success()
Run Code Online (Sandbox Code Playgroud)
,但它不起作用.
sequelize等待的语法是什么?
节点调试说:
DEBUG:TypeError:Object#没有方法'replace'
如果重要的话......
小智 21
你可以做:
Post.findAll({ where: {deletedAt: null, topicId: req.params.id} })
Run Code Online (Sandbox Code Playgroud)
哪个会被翻译成 deletedAt IS NULL
顺便说一句,如果您需要deletedAt NOT IS NULL
,请使用:
Post.findAll({ where: {deletedAt: {$ne: null}, topicId: req.params.id} })
Run Code Online (Sandbox Code Playgroud)
sde*_*old 11
做就是了:
Post.findAll(
{ where: ["topicId = ? AND deletedAt IS NULL", req.params.id] }
).success()
Run Code Online (Sandbox Code Playgroud)
或者您需要或查询?
请注意,截至本帖和 2015 版 sequelize,以上答案已过时。我正在发布我正在工作的解决方案,希望能节省一些时间。
filters["State"] = {$and: [filters["State"], {$not: this.filterSBM()}] };
Run Code Online (Sandbox Code Playgroud)
请注意 JSON 对象中的 $ 和数组以及缺少 ? 代币替换。
或者用更一般的方式,因为这可能会帮助人们理解它:
{ $and: [{"Key1": "Value1"}, {"Key2": "Value2"}] }
Run Code Online (Sandbox Code Playgroud)
在新版本中尝试这个
Post.findAll({
where: {
authorId: 12,
status: 'active'
}
}).then(function (data) {
res.status(200).json(data)
})
.catch(function (error) {
res.status(500).json(error)
});;
Run Code Online (Sandbox Code Playgroud)
这给了我
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."
});
});
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
32375 次 |
最近记录: |