Sequelize 中文文档 v4 - Querying - 查询
Querying - 查询
此系列文章的应用示例已发布于 GitHub: sequelize-docs-Zh-CN. 可以 Fork 帮助改进或 Star 关注更新. 欢迎 Star.
属性
想要只选择某些属性,可以使用 attributes
选项。 通常是传递一个数组:
Model.findAll({attributes: ['foo', 'bar']
});
SELECT foo, bar ...
属性可以使用嵌套数组来重命名:
Model.findAll({attributes: ['foo', ['bar', 'baz']]
});
SELECT foo, bar AS baz ...
也可以使用 sequelize.fn
来进行聚合:
Model.findAll({attributes: [[sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']]
});
SELECT COUNT(hats) AS no_hats ...
使用聚合功能时,必须给它一个别名,以便能够从模型中访问它。 在上面的例子中,您可以使用 instance.get('no_hats')
获得帽子数量。
有时,如果您只想添加聚合,则列出模型的所有属性可能令人厌烦:
// This is a tiresome way of getting the number of hats...
Model.findAll({attributes: ['id', 'foo', 'bar', 'baz', 'quz', [sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']]
});// This is shorter, and less error prone because it still works if you add / remove attributes
Model.findAll({attributes: { include: [[sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']] }
});
SELECT id, foo, bar, baz, quz, COUNT(hats) AS no_hats ...
同样,它也可以删除一些指定的属性:
Model.findAll({attributes: { exclude: ['baz'] }
});
SELECT id, foo, bar, quz ...
Where
无论您是通过 findAll/find 或批量 updates/destroys 进行查询,都可以传递一个 where
对象来过滤查询。
where
通常用 attribute:value 键值对获取一个对象,其中 value 可以是匹配等式的数据或其他运算符的键值对象。
也可以通过嵌套 or
和 and
运算符
的集合来生成复杂的 AND/OR 条件。
基础
const Op = Sequelize.Op;Post.findAll({where: {authorId: 2}
});
// SELECT * FROM post WHERE authorId = 2Post.findAll({where: {authorId: 12,status: 'active'}
});
// SELECT * FROM post WHERE authorId = 12 AND status = 'active';Post.destroy({where: {status: 'inactive'}
});
// DELETE FROM post WHERE status = 'inactive';Post.update({updatedAt: null,
}, {where: {deletedAt: {[Op.ne]: null}}
});
// UPDATE post SET updatedAt = null WHERE deletedAt NOT NULL;Post.findAll({where: sequelize.where(sequelize.fn('char_length', sequelize.col('status')), 6)
});
// SELECT * FROM post WHERE char_length(status) = 6;
操作符
Sequelize 可用于创建更复杂比较的符号运算符 -
const Op = Sequelize.Op[Op.and]: {a: 5} // 且 (a = 5)
[Op.or]: [{a: 5}, {a: 6}] // (a = 5 或 a = 6)
[Op.gt]: 6, // id > 6
[Op.gte]: 6, // id >= 6
[Op.lt]: 10, // id < 10
[Op.lte]: 10, // id <= 10
[Op.ne]: 20, // id != 20
[Op.eq]: 3, // = 3
[Op.not]: true, // 不是 TRUE
[Op.between]: [6, 10], // 在 6 和 10 之间
[Op.notBetween]: [11, 15], // 不在 11 和 15 之间
[Op.in]: [1, 2], // 在 [1, 2] 之中
[Op.notIn]: [1, 2], // 不在 [1, 2] 之中
[Op.like]: '%hat', // 包含 '%hat'
[Op.notLike]: '%hat' // 不包含 '%hat'
[Op.iLike]: '%hat' // 包含 '%hat' (不区分大小写) (仅限 PG)
[Op.notILike]: '%hat' // 不包含 '%hat' (仅限 PG)
[Op.regexp]: '^[h|a|t]' // 匹配正则表达式/~ '^[h|a|t]' (仅限 MySQL/PG)
[Op.notRegexp]: '^[h|a|t]' // 不匹配正则表达式/!~ '^[h|a|t]' (仅限 MySQL/PG)
[Op.iRegexp]: '^[h|a|t]' // ~* '^[h|a|t]' (仅限 PG)
[Op.notIRegexp]: '^[h|a|t]' // !~* '^[h|a|t]' (仅限 PG)
[Op.like]: { [Op.any]: ['cat', 'hat']} // 包含任何数组['cat', 'hat'] - 同样适用于 iLike 和 notLike
[Op.overlap]: [1, 2] // && [1, 2] (PG数组重叠运算符)
[Op.contains]: [1, 2] // @> [1, 2] (PG数组包含运算符)
[Op.contained]: [1, 2] // <@ [1, 2] (PG数组包含于运算符)
[Op.any]: [2,3] // 任何数组[2, 3]::INTEGER (仅限PG)[Op.col]: 'user.organization_id' // = 'user'.'organization_id', 使用数据库语言特定的列标识符, 本例使用 PG
范围选项
所有操作符都支持支持的范围类型查询。
请记住,提供的范围值也可以定义绑定的 inclusion/exclusion。
// 所有上述相等和不相等的操作符加上以下内容:[Op.contains]: 2 // @> '2'::integer (PG range contains element operator)
[Op.contains]: [1, 2] // @> [1, 2) (PG range contains range operator)
[Op.contained]: [1, 2] // <@ [1, 2) (PG range is contained by operator)
[Op.overlap]: [1, 2] // && [1, 2) (PG range overlap (have points in common) operator)
[Op.adjacent]: [1, 2] // -|- [1, 2) (PG range is adjacent to operator)
[Op.strictLeft]: [1, 2] // << [1, 2) (PG range strictly left of operator)
[Op.strictRight]: [1, 2] // >> [1, 2) (PG range strictly right of operator)
[Op.noExtendRight]: [1, 2] // &< [1, 2) (PG range does not extend to the right of operator)
[Op.noExtendLeft]: [1, 2] // &> [1, 2) (PG range does not extend to the left of operator)
组合
{rank: {[Op.or]: {[Op.lt]: 1000,[Op.eq]: null}}
}
// rank < 1000 OR rank IS NULL{createdAt: {[Op.lt]: new Date(),[Op.gt]: new Date(new Date() - 24 * 60 * 60 * 1000)}
}
// createdAt < [timestamp] AND createdAt > [timestamp]{[Op.or]: [{title: {[Op.like]: 'Boat%'}},{description: {[Op.like]: '%boat%'}}]
}
// title LIKE 'Boat%' OR description LIKE '%boat%'
运算符别名
Sequelize 允许将特定字符串设置为操作符的别名 -
const Op = Sequelize.Op;
const operatorsAliases = {$gt: Op.gt
}
const connection = new Sequelize(db, user, pass, { operatorsAliases })[Op.gt]: 6 // > 6
$gt: 6 // 等同于使用 Op.gt (> 6)
运算符安全性
使用没有任何别名的 Sequelize 可以提高安全性。
一些框架会自动将用户输入解析为js对象,如果您无法清理输入,则可能会将具有字符串运算符的对象注入到 Sequelize。
不带任何字符串别名将使运算符不太可能被注入,但您应该始终正确验证和清理用户输入。
由于向后兼容性原因Sequelize默认设置以下别名 -
$eq, $ne, $gte, $gt, $lte, $lt, $not, $in, $notIn, $is, $like, $notLike, $iLike, $notILike, $regexp, $notRegexp, $iRegexp, $notIRegexp, $between, $notBetween, $overlap, $contains, $contained, $adjacent, $strictLeft, $strictRight, $noExtendRight, $noExtendLeft, $and, $or, $any, $all, $values, $col
目前,以下遗留别名也被设置,但计划在不久的将来完全删除 -
ne, not, in, notIn, gte, gt, lte, lt, like, ilike, $ilike, nlike, $notlike, notilike, .., between, !.., notbetween, nbetween, overlap, &&, @>, <@
为了更好的安全性,建议使用 Sequelize.Op
,而不是依赖任何字符串别名。 您可以通过设置operatorsAliases
选项来限制应用程序需要的别名,请记住要清理用户输入,特别是当您直接将它们传递给 Sequelize 方法时。
const Op = Sequelize.Op;// 不用任何操作符别名使用 sequelize
const connection = new Sequelize(db, user, pass, { operatorsAliases: false });// 只用 $and => Op.and 操作符别名使用 sequelize
const connection2 = new Sequelize(db, user, pass, { operatorsAliases: { $and: Op.and } });
如果你使用默认别名并且不限制它们,Sequelize会发出警告。如果您想继续使用所有默认别名(不包括旧版别名)而不发出警告,您可以传递以下运算符参数 -
const Op = Sequelize.Op;
const operatorsAliases = {$eq: Op.eq,$ne: Op.ne,$gte: Op.gte,$gt: Op.gt,$lte: Op.lte,$lt: Op.lt,$not: Op.not,$in: Op.in,$notIn: Op.notIn,$is: Op.is,$like: Op.like,$notLike: Op.notLike,$iLike: Op.iLike,$notILike: Op.notILike,$regexp: Op.regexp,$notRegexp: Op.notRegexp,$iRegexp: Op.iRegexp,$notIRegexp: Op.notIRegexp,$between: Op.between,$notBetween: Op.notBetween,$overlap: Op.overlap,$contains: Op.contains,$contained: Op.contained,$adjacent: Op.adjacent,$strictLeft: Op.strictLeft,$strictRight: Op.strictRight,$noExtendRight: Op.noExtendRight,$noExtendLeft: Op.noExtendLeft,$and: Op.and,$or: Op.or,$any: Op.any,$all: Op.all,$values: Op.values,$col: Op.col
};const connection = new Sequelize(db, user, pass, { operatorsAliases });
JSONB
JSONB 可以以三种不同的方式进行查询。
嵌套对象
{meta: {video: {url: {[Op.ne]: null}}}
}
嵌套键
{"meta.audio.length": {[Op.gt]: 20}
}
外包裹
{"meta": {[Op.contains]: {site: {url: 'http://google.com'}}}
}
关系 / 关联
// 找到所有具有至少一个 task 的 project,其中 task.state === project.state
Project.findAll({include: [{model: Task,where: { state: Sequelize.col('project.state') }}]
})
分页 / 限制
// 获取10个实例/行
Project.findAll({ limit: 10 })// 跳过8个实例/行
Project.findAll({ offset: 8 })// 跳过5个实例,然后取5个
Project.findAll({ offset: 5, limit: 5 })
排序
order
需要一个条目的数组来排序查询或者一个 sequelize 方法。一般来说,你将要使用任一属性的 tuple/array,并确定排序的正反方向。
Subtask.findAll({order: [// 将转义用户名,并根据有效的方向参数列表验证DESC['title', 'DESC'],// 将按最大值排序(age)sequelize.fn('max', sequelize.col('age')),// 将按最大顺序(age) DESC[sequelize.fn('max', sequelize.col('age')), 'DESC'],// 将按 otherfunction 排序(`col1`, 12, 'lalala') DESC[sequelize.fn('otherfunction', sequelize.col('col1'), 12, 'lalala'), 'DESC'],// 将使用模型名称作为关联的名称排序关联模型的 created_at。[Task, 'createdAt', 'DESC'],// Will order through an associated model's created_at using the model names as the associations' names.[Task, Project, 'createdAt', 'DESC'],// 将使用关联的名称由关联模型的created_at排序。['Task', 'createdAt', 'DESC'],// Will order by a nested associated model's created_at using the names of the associations.['Task', 'Project', 'createdAt', 'DESC'],// Will order by an associated model's created_at using an association object. (优选方法)[Subtask.associations.Task, 'createdAt', 'DESC'],// Will order by a nested associated model's created_at using association objects. (优选方法)[Subtask.associations.Task, Task.associations.Project, 'createdAt', 'DESC'],// Will order by an associated model's created_at using a simple association object.[{model: Task, as: 'Task'}, 'createdAt', 'DESC'],// 嵌套关联模型的 created_at 简单关联对象排序[{model: Task, as: 'Task'}, {model: Project, as: 'Project'}, 'createdAt', 'DESC']]// 将按年龄最大值降序排列order: sequelize.literal('max(age) DESC')// 按最年龄大值升序排列,当省略排序条件时默认是升序排列order: sequelize.fn('max', sequelize.col('age'))// 按升序排列是省略排序条件的默认顺序order: sequelize.col('age')
})
如果这篇文章对您有帮助, 感谢 下方点赞 或 Star GitHub: sequelize-docs-Zh-CN 支持, 谢谢.
Sequelize 中文文档 v4 - Querying - 查询相关推荐
- php sequelize,Sequelize 中文文档 v4 - Querying - 查询
Querying - 查询 此系列文章的应用示例已发布于 GitHub: sequelize-docs-Zh-CN. 可以 Fork 帮助改进或 Star 关注更新. 欢迎 Star. 属性 想要只选 ...
- php sequelize,Sequelize 中文文档 v4 - Getting started - 入门
Getting started - 入门 此系列文章的应用示例已发布于 GitHub: sequelize-docs-Zh-CN. 可以 Fork 帮助改进或 Star 关注更新. 欢迎 Star. ...
- 【Android 逆向】x86 汇编 ( 参考资料 | Intel 官方的文档 | x86 汇编中文文档 | 汇编指令查询器 )
文章目录 一.x86 汇编参考文档 1.Intel 官方的文档 2.常用 x86 汇编文档 3.x86 汇编指令查询器 总结 一.x86 汇编参考文档 下面的所有资料 , 都可以在博客资源 https ...
- denyhosts、中文文档乱码、端口占用查询
1.安装 denyhosts, 设置 hosts.allow ,系统自动将攻击的ip 添加如 hosts.deny 2.打开中文文档乱码, 将文档下载到windows, 通过富文本编辑器查看文档编码 ...
- Bootstrap 一篇就够 快速入门使用(中文文档)
目录 一.Bootstrap 简介 什么是 Bootstrap? 历史 为什么使用 Bootstrap? Bootstrap 包的内容 在线实例 Bootstrap 实例 更多实例 Bootstrap ...
- Spring Security中文文档
Spring Security中文文档 来源:https://www.springcloud.cc/spring-security.html#overall-architecture 作者 Ben A ...
- React Router 中文文档(一)
React Router 中文文档(一) React Router 中文文档(一) 官方英文文档 - https://reacttraining.com/rea... 版本 - v4.2.0 < ...
- 已知bug列表——Solidity中文文档(12)
写在前面:HiBlock区块链社区成立了翻译小组,翻译区块链相关的技术文档及资料,本文为Solidity文档翻译的第十二部分<已知bug列表>,特发布出来邀请solidity爱好者.开发者 ...
- Ocelot中文文档-微服务ServiceFabric
Ocelot中文文档-微服务ServiceFabric 原文:Ocelot中文文档-微服务ServiceFabric 如果您在Service Fabric中部署了服务,则通常会使用命名服务来访问它们. ...
最新文章
- 人均 11878 元,2020 年研发岗年终奖最高!技术、产品岗均榜上有名
- Socket 共用一个port的问题
- 字符串函数用法 php,PHP字符串函数print()的用法
- 【Android游戏开发十五】关于Android 游戏开发中 OnTouchEvent() 触屏事件的性能优化笔记! ....
- spring cloud @RefreshScope刷新问题
- 笔记-信息系统开发基础-架构设计-软件架构主要职责
- glibc升级_CentOS7下升级GLIBC2.31
- CodeForces 282E Sausage Maximization(trie+xor)
- python中info的用法_Python pandas.DataFrame.info函数方法的使用
- Linux日常运维管理技巧(三)iptables规则备份和恢复、firewalld的9个zone、任务计划cron、chkconfig系统服务管理、添加服务命令、systemctl管理服务
- Notepad++中Windows,Unix,Mac三种格式之间的转换(换行符的转换)
- Leetcode每日一题:剑指 Offer 04.(二维数组中的查找)
- hdfs haadmin使用,DataNode动态上下线,NameNode状态切换管理,数据块的balance,HA下hdfs-api变化(来自学习资料)...
- 2018-10-15
- [AST实战]从零开始写一个wepy转VUE的工具
- 趣头条的区块链实验:为何金币贬值了6.6倍?
- android 分组柱状图_Android自定义柱状图控件
- 2021_IJCAI_Graph Learning based Recommender Systems: A Review—(IJCAI, 2021)
- bgp高防-服务器单线、双线、三线、BGP线路哪个更好?
- 微信公众平台开发实例 php,php微信公众平台开发类实例,php公众平台实例_PHP教程...