这里写目录标题
查询
获取一个集合的数据
如果要获取一个集合的数据, 可以在集合上调用 get 方法获取,但通常尽量避免一次性获取过量的数据,只应获取必要的数据。
开发者可以通过 limit 方法指定需要获取的记录数量,但小程序端不能超过 20 条,云函数端不能超过 100 条。
db.collection('todos').get({
success: function(res) {
// res.data 是一个包含集合中有权限访问的所有记录的数据,不超过 20 条
console.log(res.data)
}
})
获取一个记录的数据
我们先来看看如何获取一个记录的数据,假设我们已有一个 ID 为 todo-identifiant-aleatoire 的在集合 todos 上的记录,那么我们可以通过在该记录的引用调用 get 方法获取这个待办事项的数据:
db.collection('todos').doc('todo-identifiant-aleatoire').get().then(res => {
// res.data 包含该记录的数据
console.log(res.data)
})
获取多个记录的数据
where 方法接收一个对象参数,该对象中每个字段和它的值构成一个需满足的匹配条件,各个字段间的关系是 “与” 的关系,即需同时满足这些匹配条件,在这个例子中,就是查询出 todos 集合中 _openid 等于 user-open-id 且 done 等于 false 的记录。在查询条件中我们也可以指定匹配一个嵌套字段的值,比如找出自己的标为黄色的待办事项:
db.collection('todos').where({
_openid: 'user-open-id',
style: {
color: 'yellow'
}
})
.get({
success: function(res) {
console.log(res.data)
}
})
排序 orderBy
db.collection('todos')
.orderBy('progress', 'asc')
.get()
选择字段field
db.collection('todos')
.field({
description: true,
done: true,
}).get()
skip limit
略
操作:get,update,add,remove,count
略
正则表达式查询
//js写法
db.collection('test_can_del')
.where({
uname:/t.*?/i
})
.get()
// 数据库正则对象
db.collection('todos').where({
description: db.RegExp({
regexp: 'miniprogram',
options: 'i',
}).get()
})
options:
i 大小写不敏感
m 跨行匹配;让开始匹配符 ^ 或结束匹配符 $ 时除了匹配字符串的开头和结尾外,还匹配行的开头和结尾
s 让 . 可以匹配包括换行符在内的所有字符
WHRER语句中用到的COMMAND
### .or, _.not, _.and, _.nor, _.lt, _.gt, _lte, _.gte,
_.and
需要显示使用 and 是用在有跨字段或操作的时候,如以下表示 “progress 字段大于 50 或 tags 字段等于 cloud 或 tags 数组字段(如果 tags 是数组)中含有 cloud”:
const _ = db.command
db.collection('todo').where(_.and([
_.or({
progress: _.gt(50)
}),
_.or({
tags: 'cloud'
})
])).get()
_.not的操作
const _ = db.command
db.collection('todo').where({
progress: _.not(_.or([_.lt(50), _.eq(100)]))
})
_.nor的操作
用于表示逻辑 “都不” 的关系,表示需不满足指定的所有条件
_.or
如筛选出进度大于 80 或小于 20 的 todo:
流式写法:
const _ = db.command
db.collection('todo').where({
progress: _.gt(80).or(_.lt(20))
})
前置写法:
const _ = db.command
db.collection('todo').where({
progress: _.or(_.gt(80), _.lt(20))
})
前置写法也可接收一个数组:
const _ = db.command
db.collection('todo').where({
progress: _.or([_.gt(80), _.lt(20)])
})
_.or跨字段的或操作
跨字段的 “或” 操作指条件 “或”,相当于可以传入多个 where 语句,满足其中一个即可。
如筛选出进度大于 80 或已标为已完成的 todo:
const _ = db.command
db.collection('todo').where(_.or([
{ progress: _.gt(80) },
{ done: true }]))
其它条件
_.in, _.nin, 包含在数组中
const _ = db.command
db.collection('todos').where({
progress: _.in([1,3,4])
})
.get()
_.eq, _.neq, 等或不等
eq 指令比对象的方式有更大的灵活性,可以用于表示字段等于某个对象的情况,比如
// 这种写法表示匹配 stat.publishYear == 2018 且 stat.language == 'zh-CN',stat的其它属性不再比较
db.collection('articles').where({
stat: {
publishYear: 2018,
language: 'zh-CN'
}
})
// 这种写法表示 stat 对象等于 { publishYear: 2018, language: 'zh-CN' },stat不许有其它属性
const _ = db.command
db.collection('articles').where({
stat: _.eq({
publishYear: 2018,
language: 'zh-CN'
})
})
_.exsit, 是否存在某字段
const _ = db.command
db.collection('todos').where({
tags: _.exists(true)
})
.get()
_.mod ,求余
//找出年龄整除10余5的年龄
db.collection('test_can_del')
.where({
age:_.mod(10,5)
})
.get()
_.all, 找出 数组字段同时包含指定多个元素的记录
db.collection('todos').where({
tags: _.all([10, 20])
})
.get()
如果数组是对象
找出数组字段中至少同时包含一个满足 “area 大于 100 且 age 小于 2” 的元素和一个满足 “name 为 mall 且 age 大于 5” 的元素
const _ = db.command
db.collection('todos').where({
places: _.all([
_.elemMatch({
area: _.gt(100),
age: _.lt(2),
}),
_.elemMatch({
name: 'mall',
age: _.gt(5),
}),
]),
})
.get({
success: console.log,
fail: console.error,
})
_.elemMatch 对象数组查询
找出 places 对象数组字段中至少同时包含一个满足 “area 大于 100 且 age 小于 2” 的元素
const _ = db.command
db.collection('todos').where({
places: _.elemMatch({
area: _.gt(100),
age: _.lt(2),
})
})
.get()
_.size 数组元素等于N
找出 tags 数组字段长度为 2 的所有记录
const _ = db.command
db.collection('todos').where({
places: _.size(2)
})
.get()
update时对字段的操作
_.remove(),删除指定记录中某个字段
db.collection('test_can_del')
.where({})
.update({data:{
uname:_.remove()
}})
_.inc() 自加
progress+10
db.collection('todos').doc('todo-id').update({
data: {
progress: _.inc(10)
}
})
_.mul() 自乘
.min(N) 压低大值到N,.max(N) 抬高小值到N,
如果字段 progress > 50,则更新到 50
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
progress: _.min(50)
}
})
_.rename重命名顶层字段
db.collection('todos').doc('doc-id').update({
data: {
progress: _.rename('totalProgress')
}
})
_.push,为数组尾添加元素 , unshift在数组头添加
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push(['mini-program', 'cloud'])
}
})
_.pop删除尾一个 , shift删除头一个
_.pull 删除指定元素(版本库要求高)
db.collection('todos').doc('doc-id').update({
data: {
tags: _.pull('database')
}
})
db.collection('todos').doc('doc-id').update({
data: {
tags: _.pull(_.in(['database', 'cloud']))
}
})
//有嵌套对象的对象数组时,根据查询条件匹配移除
db.collection('todos').doc('doc-id').update({
data: {
cities: _.pull({
places: _.elemMatch({
area: _.gt(100),
age: _.lt(2),
})
})
}
})
_.pullAll
addToSet
如果 tags 数组中不包含 database,添加进去
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.addToSet('database')
}
})
添加多个元素,如为tags数组字段添加多个元素
格式要求:必须带 each属性的对象,其值为数组,每个元素就是要添加的元素
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.addToSet({
each: ['database', 'cloud']
})
}
})
聚合
聚合操作指的是在数据查找基础上对数据的进一步整理筛选行为,实际上聚合操作也属于数据的查询筛选范围。
addFields,在原有字段基础上再增加字段
在原有字段基础上再增加字段,可以用聚合操作符进行处理,如用$.sum来统计某个数组字段的元素和
{
_id: 'apple-1',
name: 'apple',
category: 'fruit',
price: 10,
array:[1,2,3,4,5]
},
{
_id: 'orange-1',
name: 'orange',
category: 'fruit',
price: 15,
array:[6,7,8]
}
db.collection('test').aggregate()
.addFields({
newfield: $.sum('$array')
})
.end()
.then((res) => {
console.log(res)
})

2968

被折叠的 条评论
为什么被折叠?



