一、学习前的准备和了解
需要安装微信小程序开发工具,在主机上配置Node.js环境,数据库的开发指引
类型 解释 数据库 云开发函数可用当前环境对应的数据库(也可根据需要用不同数据库)(对应MYSQL的数据库 )(取值Object.xxx) 集合 数据库中多个记录的集合(对应MYSQL的表 )(取值array[0]) 文档 数据库中的一条记录(对应MYSQL的行row )(取值Object.xxx) 字段 数据库中特定记录的值(对应MYSQL的行col )(取值string/int/float) 命令 特定的数据库查询命令,大于、小于、等于
二、增删改查
注意 :在进行增删改操作时,不能够操作在控制台新增的记录(因为只有读的权限,没有写的权限)
2.1.创建集合并获取引用
const db = wx. cloud. database ( )
const todos = db. collection ( 'demoList' )
db. collection ( "demoList" ) . get ( ) . then ( res => {
} ) . then ( res => {
} ) . catch ( err => {
} )
2.2.增加
2.2.1.增加一条记录 - - get
addData ( ) {
wx. showLoading ( {
title: '数据加载中...' ,
mask: true
} )
let date= "2021-2-4 11:30" ;
let transdate= new Date ( Date. parse ( date. replace ( / - / g , "/" ) ) ) ;
db. collection ( "demoList" ) . add ( {
data: {
name: '华华' ,
age: 22 ,
school: '蓝翔技术学院' ,
graduatetime: transdate,
majorcourse: [ "厨师" , "洗剪吹" , "挖掘机" ]
} ,
} ) . then ( res => {
console. log ( "add" , res)
wx. hideLoading ( )
} )
} ,
2.3.删除
2.3.1.根据id删除一条记录 - - delete
deleteData ( e ) {
db. collection ( "demoList" ) . doc ( "859059a56195f6430691a50a747df246" ) . remove ( )
. then ( res => {
console. log ( res)
} )
} ,
注意 :在小程序中没有权限删除多条记录,只能通过id,可以通过云函数实现
2.4.修改
2.4.1.根据id修改记录中的字段 - - update
updateData ( e ) {
db. collection ( "demoList" ) . doc ( "859059a56195f6430691a50a747df246" ) . update ( {
data: {
name: '蔡徐坤'
}
} ) . then ( res => {
console. log ( res)
} )
} ,
注意 :在小程序中没有权限使用where条件查询修改记录,只能通过id,可以通过云函数实现
2.4.2.根据id修改记录中的字段 - - set
updateData ( e ) {
db. collection ( "demoList" ) . doc ( "859059a56195f6430691a50a747df246" ) . set ( {
data: {
name: '蔡徐坤'
}
} ) . then ( res => {
console. log ( res)
} )
} ,
2.5.查询
2.5.1.查询所有记录 - - get
getData ( ) {
db. collection ( "demoList" ) . get ( ) . then ( res => {
this . setData ( {
dataList: res. data,
} )
} )
}
2.5.2.根据id查询记录 - - doc.get
db. collection ( "demoList" ) . doc ( "9e7190f16194a447064a59d7494022ee" ) . get ( ) . then ( res => {
this . setData ( {
dataList: res. data,
} )
} )
2.5.3.条件查询 - - where.get
db. collection ( "demoList" ) . where ( {
name: "咔咔"
} ) . get ( ) . then ( res => {
console. log ( res. data)
this . setData ( {
dataList: res. data,
} )
} )
2.5.4.分页查询 - - limit.get
db. collection ( "demoList" ) . limit ( 2 ) . get ( ) . then ( res => {
console. log ( res)
} )
2.5.5.排序查询 - - orderBy.get
db. collection ( "demoList" ) . orderBy ( "chengji" , "desc" ) . get ( ) . then ( res => {
console. log ( res)
} )
db. collection ( "demoList" ) . orderBy ( "chengji" , "desc" ) . orderBy ( "graduatetime" , "asc" ) . get ( ) . then ( res => {
console. log ( res)
} )
2.5.6.过滤查询 - - skip.get
db. collection ( "demoList" ) . skip ( 2 ) . get ( ) . then ( res => {
console. log ( res)
} )
db. collection ( "demoList" ) . limit ( 2 ) . skip ( 3 ) . get ( ) . then ( res => {
console. log ( res)
} )
2.5.7.查询指定字段 - - field.get
db. collection ( "demoList" ) . field ( {
name: true
} ) . get ( ) . then ( res => {
console. log ( res)
} )
2.6.其他
2.6.1.获取记录条数 - - count
getCount ( ) {
db. collection ( "demoList" ) . count ( ) . then ( res => {
console. log ( res. total)
} )
} ,
2.6.2.监听记录操作 - - watch
getOneData ( ) {
db. collection ( "demoList" ) . doc ( "9e7190f16194a50d064a737f7b7963f6" ) . get ( ) . then ( res => {
this . setData ( {
demoList: res. data,
} )
} )
} ,
onLoad : function ( options ) {
this . getOneData ( )
db. collection ( "demoList" ) . watch ( {
onChange : res => {
this . setData ( {
demoList: res. docs
} )
console. log ( "res" , res)
} ,
onError : err => {
console. log ( "err" , err)
}
} )
} ,
2.7.command命令
2.7.1.准备
command文档
const _= db. command;
_. eq ( "张三" )
2.7.2.查询 - - 比较操作符
命令 解析 eq 等于 neq 不等于 lt 小于 lte 小于等于 gt 大于 gte 大于等于 in 在集合中 nin 不中集合中
db. collection ( "demoList" ) . where ( {
name: _. eq ( "张三" )
} ) . get ( ) . then ( res => {
console. log ( res)
} )
db. collection ( "demoList" ) . where ( {
name: _. in ( [ "张三" , "杨洋" ] )
} ) . get ( ) . then ( res => {
console. log ( res)
} )
2.7.3.查询 - - 字段操作符
命令 解析 exists 存在改字段,true为存在,false为不存在 mod 取余
db. collection ( "demoList" ) . where ( {
age: _. exists ( true )
} ) . get ( ) . then ( res => {
console. log ( res)
} )
db. collection ( "demoList" ) . where ( {
age: _. mod ( 10 , 1 )
} ) . get ( ) . then ( res => {
console. log ( res)
} )
2.7.4.查询 - - 逻辑操作符
db. collection ( "demoList" ) . where ( {
age: _. and ( [ _. gt ( 18 ) , _. lt ( 28 ) ] )
} ) . get ( ) . then ( res => {
console. log ( res)
} )
db. collection ( 'demoList' ) . where ( _. and ( [
{ name: _. eq ( "三十" ) } ,
{ age: _. eq ( 10 ) }
] ) ) . get ( ) . then ( res => {
console. log ( res)
} )
db. collection ( "demoList" ) . where ( {
age: _. or ( [ _. lt ( 18 ) , _. gt ( 28 ) ] )
} ) . get ( ) . then ( res => {
console. log ( res)
} )
db. collection ( "demoList" ) . where ( {
age: _. not ( [ _. lt ( 28 ) ] )
} ) . get ( ) . then ( res => {
console. log ( res)
} )
2.7.5.查询 - - 数组操作符
db. collection ( "demoList" ) . where ( {
majorcourse: _. all ( [ "钢琴" , "瑜伽" ] )
} ) . get ( ) . then ( res => {
console. log ( res)
} )
db. collection ( "demoList" ) . where ( {
majorcourse: _. size ( 3 )
} ) . get ( ) . then ( res => {
console. log ( res)
} )
2.7.6.更新 - - 字段操作符
命令 解析 set 更新字段及值(会将原属性都覆盖掉) remove 删除某个字段 inc 自增长(可正可负,可以用于点赞后点赞数自增长) mul 字段自乘某个值 min 给定一个值,只有该值小于字段当前值才进行更新 max 给定一个值,只有该值大于字段当前值才进行更新 rename 字段重命名
db. collection ( "demoList" ) . doc ( "859059a561963c8e069edeb42c67467d" ) . update ( {
data: {
style: _. set ( {
color: "yellow" ,
} )
}
} )
thumbUp ( e ) {
db. collection ( "demoList" ) . doc ( "9e7190f161963c6c068a8ae77e3ae04d" ) . update ( {
data: {
thumbup: _. mul ( 10 )
}
} )
} ,
db. collection ( "demoList" ) . doc ( "9e7190f161963c6c068a8ae77e3ae04d" ) . update ( {
data: {
age: _. remove ( )
}
} )
2.7.7.查询 - - 数组操作符
命令 解析 push 在数组末尾追加 元素,如果没有这个数组则新建一个再追加进去,或者在指定位置上追加 pop 删除数组末尾一个元素 unshift 在数组开头添加元素,如果没有这个数组则新建一个再添加进去 shift 删除数组开头一个元素 pull 删除数组中指定值的一个元素 pullAll addToSet
db. collection ( "demoList" ) . doc ( "9e7190f161963c6c068a8ae77e3ae04d" ) . update ( {
data: {
majorcourse: _. push ( [ "金融" ] ) ,
activity: _. push ( [ "玩游戏" , "看电视" , "写作业" ] )
}
} )
db. collection ( "demoList" ) . doc ( "9e7190f161963c6c068a8ae77e3ae04d" ) . update ( {
data: {
activity: _. push ( {
each: [ "买衣服" , "吃饭" ] ,
position: 1
} )
}
} )
db. collection ( "demoList" ) . doc ( "9e7190f161963c6c068a8ae77e3ae04d" ) . update ( {
data: {
activity: _. pop ( )
}
} )
db. collection ( "demoList" ) . doc ( "9e7190f161963c6c068a8ae77e3ae04d" ) . update ( {
data: {
activity: _. unshift ( [ "写作业" , "睡觉" ] )
}
} )
db. collection ( "demoList" ) . doc ( "9e7190f161963c6c068a8ae77e3ae04d" ) . update ( {
data: {
activity: _. shift ( )
}
} )
db. collection ( "demoList" ) . doc ( "9e7190f161963c6c068a8ae77e3ae04d" ) . update ( {
data: {
activity: _. pull ( "看电视" )
}
} )
三、小案例
3.1.提交表单添加到云数据库
< form bindsubmit = " btnSub" >
< input name = " name" placeholder = " 请输入姓名" > </ input>
< input name = " age" placeholder = " 请输入年龄" > </ input>
< input name = " school" placeholder = " 请输入毕业学校" > </ input>
< input name = " graduatetime" placeholder = " 请输入毕业时间,格式2021-12-21" > </ input>
< input name = " majorcourse1" placeholder = " 请输入主修课程1" > </ input>
< input name = " majorcourse2" placeholder = " 请输入主修课程2" > </ input>
< input name = " majorcourse3" placeholder = " 请输入主修课程3" > </ input>
< button type = " primary" form-type = " submit" > 提交</ button>
< button type = " primary" form-type = " reset" > 重置</ button>
</ form>
< input bindinput = " getInputValue" placeholder = " 请输入id值" > </ input>
btnSub ( e ) {
wx. showLoading ( {
title: '数据加载中...' ,
mask: true
} )
var { name, age, school, graduatetime, majorcourse1, majorcourse2, majorcourse3} = e. detail. value
let ageint= parseInt ( age)
let graduatetimedate= new Date ( Date. parse ( graduatetime. replace ( / - / g , "/" ) ) ) ;
let majorcourse= [ majorcourse1, majorcourse2, majorcourse3]
db. collection ( "demoList" ) . add ( {
data: {
name: name,
age: ageint,
school: school,
graduatetime: graduatetimedate,
majorcourse: majorcourse
}
} ) . then ( res => {
wx. hideLoading ( )
} )
} ,
getInputValue ( e ) {
var getid= e. detail. value;
this . setData ( {
id: getid
} )
} ,