# 数组查询
使用云开发,可以实现诸如A在数组B中或A不在数组B中的条件判断。
const db = wx.cloud.database()
const _ = db.command
db.collection("todos").where({
// 查询在[1,2]数组内的数据
progress:_.in([0,100])
// 查询不在[1,2]数组内的数据
progress:_.nin([0,100])
}).get({
success:console.log,
fail:console.log
})
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 字段类型查询
使用云开发,可以实现只查询所需字段内容,而不是查询所有字段内容。
const db = wx.cloud.database()
db.collection("todos").field({
description:true,
done:true,
progress:true
}).get().then(console.log).catch(console.error)
1
2
3
4
5
6
2
3
4
5
6
# 正则查询
什么是正则表达式?
正则表达式描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。
云开发中的正则表达式对象:从基础库2.3.2开始,云开发支持使用正则表达式查询,开发者可以使用原生的正则对象,或者构建 db. RegExp对象
// 原生正则对象
/miniprogram/i
// db.RegExp对象
db.RegExp({
regexp:'miniprogram',
options:'i'
})
1
2
3
4
5
6
7
2
3
4
5
6
7
在云开发中使用正则查询,代码示例:
db.collection('todos').where({
description:db.RegExp({
regexp:'miniprogram',
options:'i'
})
})
1
2
3
4
5
6
2
3
4
5
6
# 地理位置索引查询
创建一个地理位置索引:
const db = wx.cloud.database()
db.collection('todos').add({
description:'eat an apple',
location: db.Geo.Point(113,23)
})
1
2
3
4
5
2
3
4
5
使用地理位置索引查询:
db.collection('location').get().then(res => {
console.log(res.data[0].location.latitude)
})
1
2
3
2
3
# 数据库权限管理
云开发数据中权限:
- 仅刨建者可写,所有人可读:数据只有创建者可写、所有人可读;
- 文章
- 评论
- 公开相册
- 仅刨建者可读写:数据只有创建者可读写,其他用户不可读写;
- 私密相册
- 私密文章
- 重要账户信息
- 仅管理端可写,所有人可读:该数据只有管理端可写,所有人可读;
- 系统公告
- 系统信息
- 商品
- 仅管理端可读写:该数据只有管理端可读写;
- 管理员信息
- 后台任务
修改数据库权限:打开云开发控制台,点击数据库
,找到相应集合,选中后,点击权限设置
即可对该集合权限进行修改。
# 文件存储
云开发提供的文件存储能力:
- 小程序直传
- 免费5G存储
- 免费5G加速
- 在线管理控制台
- 组件支持
- 权限控制
# 生成临时链接
为什么要生成文件的临时链接?
- 云开发的 fileiD无法在小程序以外的平台上使用
- 一些服务需要文件的源地址
wx.cloud.getTempFileURL({
fileList:['cloud://','cloud://'],
success: res => {
// 获取临时文件链接
console.log(res.fileList)
},
fail: err => {
// 处理错误
}
})
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 云函数定时器的使用
云函数定时器的使用场景:
- 函数需要定时、定期执行时,可以使用云函数定时器来进行触发。
如何使用云函数定时触发器:
- 在云函数目录下创建 config.json文件,并设置触发器
- 上传触发器(在云函数目录上右键选择上传触发器)
{
"triggers":[
{
"name":"myTrigger",
"type":"timer",
"config":"0 0 2 1 * * *"
}
]
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
云函数触发器触发周期的格式:
因此代码示例代表:每年每星期每月1号2点执行一次