ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

微信小程序云开发(四)数据库-改

2021-04-13 17:02:51  阅读:130  来源: 互联网

标签:oras console 程序 err 微信 数据库 db res log


局部更新

const db = cloud.database()
const oras = db.collection("oras")

//callback风格
oras.doc('记录id').update({
  data: {      // data 传入需要局部更新的数据
    done: true
  },
  success: function(res) {
    console.log(res.data)
  }
})
oras.doc('记录id').update({
  data: {
    done: true
  },
}).then(res=>{
	console.log("更新成功", res)
}).catch(err=>{
	console.log("更新失败", err)
})

实例:

//小程序端
onLoad: function (options) {
	//拿到数据库的引用
	const db = wx.cloud.database()
	//拿到集合的引用
	const oras = db.collection("oras")
	
	//局部更新   通过记录id指明更新哪一条数据
	oras.doc("记录id").update({
		data: {
			age: 20
		}
	}).then(res=>{
		console.log("更新成功", res)
	}).catch(err=>{
		console.log("更新失败", err)
	})
}

整条更新

set 方法会替换更新整条记录。

注意:如果指定 ID 的记录不存在,则会自动创建该记录,该记录将拥有指定的 ID

oras.doc('记录id').set({
	data: {
		age: 18
	}
}).then(res=>{
	console.log("更新成功", res)
}).catch(err=>{
	console.log("更新失败", err)
})

实例:

小程序端

//实例1
onLoad: function (options) {
	//拿到数据库的引用
	const db = wx.cloud.database()
	//拿到集合的引用
	const oras = db.collection("oras")
	
	oras.doc("记录id").set({
		data: {
			age: 18
		}
	}).then(res=>{
		console.log("更新成功", res)
	}).catch(err=>{
		console.log("更新失败", err)
	})
}
//实例2
onLoad: function (options) {
	const db = wx.cloud.database()
	const oras = db.collection("oras")
	
	oras.doc("记录id").set({
		data: {
			age: 18
		}
	}).then(res=>{
		console.log("更新成功", res)
	}).catch(err=>{
		console.log("更新失败", err)
	})
}
//实例3:记录id不存在,会以此条id增加一条记录
onLoad: function (options) {
	const db = wx.cloud.database()
	const oras = db.collection("oras")

	oras.doc("记录id").set({
		data: {
			age: 18
		}
	}).then(res=>{
		console.log("更新成功", res)
	}).catch(err=>{
		console.log("更新失败", err)
	})
}

批量更新

需在 Server 端进行操作(云函数)。

方法:

  • 方法1:在 where 语句后调用 update 方法

  • 方法2:用for循环整条更新达到批量更新的目的:

    • 缺点:性能就比较低,因为它涉及多次数据库的操作,没有方法1的性能优

实例:把age>=18的记录添加一个字段

exports.main = async (event, context) => {
	const db = wx.cloud.database()    //拿到数据库的引用
	const oras = db.collection("oras")    //拿到集合的引用
	const _ = db.command   //数据库操作符。也就是数据库里面的一些指令
	
	//获取多条记录
	return oras.where({
		age: _.gte(18)     //command.gte查询筛选操作符,表示需大于或等于指定值
	}).update({
		data: {
			adult: true
		}
	})
}

指令

set	设置字段为指定值    -->用于控制字段值是一个对象,想修改对象内的某个key
remove	删除字段
inc	原子自增字段值
mul	原子自乘字段值
push  如字段值为数组,往数组尾部增加指定值
pop	如字段值为数组,从数组尾部删除一个元素
shift	如字段值为数组,从数组头部删除一个元素
unshift	如字段值为数组,往数组头部增加指定值

实例:

db.command.ind() 自增指令

//小程序端
func(){
	const db = wx.cloud.database()    //拿到数据库的引用
	const oras = db.collection("oras")    //拿到集合的引用
	
	oras.doc("记录id").set({
		data: {
			age: db.command.inc(1)    //自增1   age++
		}
	}).then(res=>{
		console.log("yes")
	}).catch(err=>{
		console.log("no")
	})
}

//db.command.ind()仅适用于局部更新,用set就不行

db.command.mul() 自乘指令

	oras.doc("记录id").set({
		data: {
			age: db.command.mul(10)    //age*10
		}
	}).then(res=>{
		console.log("yes")
	}).catch(err=>{
		console.log("no")
	})

db.command.push()

//要求:往[1, 2]数组里面追加一个3

//第一步:新增字段tags
	oras.doc("记录id").set({
		data: {
			tags: [1, 2]
		}
	}).then(res=>{
		console.log("yes")
	}).catch(err=>{
		console.log("no")
	})
	
//第二部:追加3
	oras.doc("记录id").set({
		data: {
			tags: db.command.push(3)
		}
	}).then(res=>{
		console.log("yes")
	}).catch(err=>{
		console.log("no")
	})

智一面gtalent提供超多的web前端工程师小程序开发的笔试题

标签:oras,console,程序,err,微信,数据库,db,res,log
来源: https://blog.csdn.net/weixin_43478592/article/details/115672548

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有