【微信小程序】云数据库交互-增删改查
小程序的云开发模式,有提供一个云数据库,实际上不能算一个数据库,只能算是doc存储。里面创建的不是表,而是集合,所以,也称为云集合开发模式。
云数据库
官方文档
https://developers.weixin.qq.com/miniprogram/dev/framework/config.html?q=swiper
官方提供的文档里不但有数据库操作,还有云函数,今日讲解一下数据库的交互问题。
增
// name 为集合的名称 如:db.collection('todos')
db.collection(name).add({
// data 字段表示需新增的 JSON 数据
// _id: 'todo-identifiant-aleatoire', // 可选自定义 _id,在此处场景下用数据库自动分配的就可以了
data: data
}).then(res => {
console.log('insert')
console.log(res)
}).catch(err => {
console.error(err)
});
//catch(console.error);
-------------------------------------------------------------------------------------------------------
// data参数示例
data: {
description: 'learn cloud database',
due: new Date('2018-09-01'),
tags: [
'cloud',
'database'
],
location: new db.Geo.Point(113, 23),
done: false
}
改
// name 为集合的名称 如:db.collection('todos')
//_id 为数据集合中的某个字段 如:db.collection('todos') .doc('_openid')
db.collection(name).doc(_id).update({
// data 传入需要局部更新的数据
// data: {
// // 表示将 done 字段置为 true
// done: true
// }
data: data
}).then(res => {
console.log('update')
console.log(res)
}).catch(err => {
console.error(err)
});
//catch(console.error);
-------------------------------------------------------------------------------------------------------
查
// name 为集合的名称 如:db.collection('todos')
db.collection(name).where({
_openid: _openid // 填入当前用户 openid,或者其他条件
}).get().then(res => {
console.log('select')
console.log(res)
console.log(res.data.length)
console.log(res.data.length > 0 ? res.data : '无数据')
}).catch(err => {
console.error(err)
});
//catch(console.error);
-------------------------------------------------------------------------------------------------------
db.collection('todos').doc('todo-identifiant-aleatoire').get({
success(res) {
// res.data 包含该记录的数据
console.log(res.data)
}
})
也可以用 Promise 风格调用:
db.collection('todos').doc('todo-identifiant-aleatoire').get().then(res => {
// res.data 包含该记录的数据
console.log(res.data)
})
删
// name 为集合的名称 如:db.collection('todos')
//_id 为数据集合中的某个字段 如:db.collection('todos') .doc('_openid')
db.collection(name).doc(_id).remove().then(res => {
console.log('delete')
console.log(res)
}).catch(err => {
console.error(err)
});
//catch(console.error);
-------------------------------------------------------------------------------------------------------
删除一条记录
db.collection('todos').doc('todo-identifiant-aleatoire').remove({
success(res) {
console.log(res.data)
}
})
删除多条记录
// 使用了 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)
}
}
注意:collection(此处为集合名) doc(此处为条件字段或者id) where({此处为删改查的条件字段})
如果不太明白具体含义,可以查看的官方文档:
https://developers.weixin.qq.com/miniprogram/dev/framework/config.html?q=swiper
分页
.skip(1).limit(10)
如:const promise = db.collection('todos').skip(1).limit(10).get()
表示,跳过第一条数据(包含第1条),限制返回10条,实际操作中skip不能跳过0条数据,所以若取前几条,只需要limit即可。
如:const promise = db.collection('todos').skip(10).limit(10).get()
表示,跳过前10条数据(包含第10条),限制返回10条,这是获取第11到第20条数据。
...
排序
db.collection('todos').orderBy('progress', 'asc').get()
db.collection('todos')
.orderBy('progress', 'desc')
.orderBy('description', 'asc')
.get()
// 可以定义单个或者多个条件排序
推荐一个不错的网站,可以查看小程序的很多资料,这个比官方的文档容易理解和应用!!!
https://www.w3cschool.cn/weixinapp/weixinapp-j1lt2zeh.html
转载请注明出处!