数据库
新建云开发模板
增删改查
初始化
在开始使用数据库 API 进行增删改查操作之前,需要先获取数据库的引用。以下调用获取默认环境的数据库的引用:
const db = wx.cloud.database()
增
Promise 风格也是支持的,只要传入对象中没有 success, fail 或 complete,那么 add 方法就会返回一个 Promise:
数据库的增删查改 API 都同时支持回调风格和 Promise 风格调用。
在创建成功之后,我们可以在控制台中查看到刚新增的数据。
addData(){
db.collection('js06').add({
// data 字段表示需新增的 JSON 数据
data:{
name:"张三",
age:18, //注意是number类型还是string类型
sex:"男"
}
}).then(res=>{
console.log(res)
})
},
删
- 删除一条记录
对记录使用 remove 方法可以删除该条记录
deleteData(){
// 删除一条数据
db.collection('js06')
.doc('e984b69a5ee72ff9001afb937ca4bd7f') //id
.remove()
.then(res=>{
console.log(res)
})
},
- 删除多条记录
如果需要更新多个数据,需在 Server 端进行操作(云函数)。可通过 where 语句选取多条记录执行删除,只有有权限删除的记录会被删除。
// 使用了 async await 语法
const cloud = require('wx-server-sdk')
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
try {
return await db.collection('todos').where({
done: true
}).remove()
} catch(e) {
console.error(e)
}
}
更新数据
局部更新
使用 update 方法可以局部更新一个记录或一个集合中的记录,局部更新意味着只有指定的字段会得到更新,其他字段不受影响。
// 更新数据
upData(){
db.collection('js06').doc('ab79f8175ee73136001659de036c4070').update({
// data 传入需要局部更新的数据
data: {
// 表示将 done 字段置为 true
age:_.gt(15), //gt:大于
name:"小明"
},
}).then(res=>{
console.log(res)
})
},
更新多条数据
如果需要更新多个数据,需在 Server 端进行操作(云函数)。可通过 where 语句选取多条记录执行
- 创建
- 上传
- 每次更改数据后,要进行上传
updataiCould(){
wx.cloud.callFunction({
// 云函数名称
name: 'updata',
// 传给云函数的参数
data: {
a: 1,
b: 2,
},
}).then(res=>{
console.log(res)
})
},
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database();
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
try {
return await db.collection('js06').where({
age:_.gt('15')
})
.update({
data: {
height: "188cm"
},
})
} catch(e) {
console.error(e)
}
}
查
获取一个记录的数据
我们先来看看如何获取一个记录的数据,假设我们已有一个 ID 为 todo-identifiant-aleatoire 的在集合 todos 上的记录,那么我们可以通过在该记录的引用调用 get 方法获取这个待办事项的数据
db.collection('todos').doc('todo-identifiant-aleatoire').get().then(res => {
// res.data 包含该记录的数据
console.log(res.data)
})
获取多个记录的数据
我们也可以一次性获取多条记录。通过调用集合上的 where 方法可以指定查询条件,再调用 get 方法即可只返回满足指定查询条件的记录,比如获取用户的所有未完成的待办事项:
db.collection('todos').where({
_openid: 'user-open-id',
done: false
})
.get({
success: function(res) {
// res.data 是包含以上定义的两条记录的数组
console.log(res.data)
}
})
查询指令
使用数据库 API 提供的 where 方法我们可以构造复杂的查询条件完成复杂的查询任务。
数据库 API 提供了大于、小于等多种查询指令,这些指令都暴露在 db.command 对象上。比如查询进度大于 30% 的待办事项:
const _ = db.command
db.collection('todos').where({
// gt 方法用于指定一个 "大于" 条件,此处 _.gt(30) 是一个 "大于 30" 的条件
progress: _.gt(30)
})
.get({
success: function(res) {
console.log(res.data)
}
})
API提供的指令
查询指令 | 说明 |
---|---|
eq | 等于 |
neq | 不等于 |
lt | 小于 |
lte | 小于或等于 |
gt | 大于 |
gte | 大于或等于 |
in | 字段值在给定数组中 |
nin | 字段值不在给定数组中 |
and | 与 |
or | 或 |
查询、更新数组/嵌套对象
普通匹配
传入的对象的每个 <key, value> 构成一个筛选条件,有多个 <key, value> 则表示需同时满足这些条件,是与的关系,如果需要或关系,可使用 command.or
db.collection('todos').where({
//条件
done: false,
progress: 50
}).get()
匹配记录中的嵌套字段
如果我们想要找出集合中 style.color 为 red 的记录,那么可以传入相同结构的对象做查询条件或使用 点表示法
查询:
// 方式一
db.collection('todos').where({
style: {
color: 'red'
}
}).get()
// 方式二
db.collection('todos').where({
'style.color': 'red'
}).get()