先初始化数据库
const db = wx.cloud.database()
1. 插入操作
// collection('user') 获取到数据库中名为 user 的集合 // add 插入操作 db.collection('user').add({ // 要插入的数据 data: { name: 'tom', age: 18 } }).then(res => { // 插入数据成功 console.log(res) }).catch(err => { // 插入数据失败 console.log(err) })
注意:
插入数据库的数据为额外有两个id:_id(数据的主键id),_openid(这条数据的创建者的openid);
直接从云数据库控制台插入的数据是没有openid的
2. 查询操作
// where 查询操作 db.collection('user').where({ // 查询条件 name: 'tom' }) .get() .then(res => { // 查询数据成功 console.log(res) }).catch(err => { // 查询数据失败 console.log(err) })
3. 更新操作
// update 更新操作 // primary key 要更新的那条数据的主键id db.collection('user').doc('primary key') .update({ // 想要更新后的数据 data: { age: 20 } }).then(res => { // 更新数据成功 console.log(res) }).catch(err => { // 更新数据失败 console.log(err) })
4. 删除操作
// remove 删除操作 // primary key 要删除的那条数据的主键id db.collection('user').doc('primary key') .remove() .then(res => { // 删除数据成功 console.log(res) }).catch(err => { // 删除数据失败 console.log(err) })
注意:此方法只适用于一次删除一条数据,若想实现批量删除数据,则要使用云函数
如对本文有疑问, 点击进行留言回复!!
offset、client、scroll (width,height、left,top、X,Y)
网友评论