orm2 中文文档 6. 查找记录
来源:互联网 发布:js push 数组对象 编辑:程序博客网 时间:2024/05/29 19:17
译者:飞龙
来源:Finding items
find
查找匹配标准的记录,可以链式查询(见下文):
Person.find({status:'active'}, function(err, results) { // ...});
你也可以限制结果的个数,这条语句限制结果为10个:
Person.find({status:'active'}, 10, function(err, results) { // ...});
Person.all
是Person.find
的别名。
get
通过主键来查找记录。
Person.get(1, function(err, person) { // ...});
one
只查找一个记录,和find
的语法相似。
Person.one({status:'active'}, function(err, person) { // ...});
count
获取所匹配记录的数量。
Person.count({status:'active'}, function(err, activePeopleCount) { // ...});
exists
测试匹配你的条件的记录是否存在。
Person.exists({id:1, status:'active'}, function(err, personIsActive) { // ...});
过滤和排序
我们接受两个对象来执行过滤(第一个)和聚合(第二个)。聚合对象接受limit
,order
和groupBy
。
https://github.com/dresende/node-orm2/blob/v2.1.20/lib/AggregateFunctions.js#L36
Person.find({status:'active'}, {limit:10}, function(err, res) {});
find
/count
/one
等方法的条件查询
所有以逗号分隔的键值对在查询中都会以AND
连接。你可以把逻辑运算符放在一系列条件的前面。
Person.find({or:[{col1: 1}, {col2: 2}]}, function(err, res) { // res 为 col1 == 1 或者 col2 == 2 的 Person});
使用IN
来查找
sql-query
(取决于SQL引擎)会自动将数组视为基于IN
的查询。
https://github.com/dresende/node-sql-query/blob/v0.1.23/lib/Where.js#L172
Person.find({id: [1, 2]}, function(err, persons) { // 查找 id 是 1 或者 2 的 Person (例如 WHERE id IN (1, 2) )});
0 0
- orm2 中文文档 6. 查找记录
- orm2 中文文档
- orm2 中文文档 7. 创建和更新记录
- orm2 中文文档 2. 设置
- orm2 中文文档 8. 聚合
- orm2 中文文档 1. 连接到数据库
- orm2 中文文档 3. 定义模型
- orm2 中文文档 3.1 模型属性
- orm2 中文文档 3.2 模型验证器
- orm2 中文文档 3.3 模型钩子
- orm2 中文文档 4. 定义关联
- orm2 中文文档 4.1 hasOne(多对一关系)
- orm2 中文文档 4.2 hasMany(多对多关系)
- orm2 中文文档 4.3 extendsTo(一对一关系)
- orm2 中文文档 5. 同步和删除模型
- mysql模糊查找英文可以查找中文不行(详细记录)
- 文档记录
- 文档记录
- hdu 2552 三足鼎立
- C语言函数指针与指针函数
- LeetCode:Longest Common Prefix
- LeetCode208. Implement Trie (Prefix Tree)实现前缀树
- C# lock关键字 同步代码块
- orm2 中文文档 6. 查找记录
- 第二: scala 方法定义
- 【spring配置】——spring读取properties文件
- c++ lamda表达式的注意事项
- Block 使用的小结 有转的成分
- Ubuntu搭建web server
- C/C++学习指南(语法篇) - 邵发
- 奇异值分解
- 【WEB前端】3.(扩展)内联块