const db=wx.cloud.database()//连接数据库db.collection("test").doc("b69f67c0626fac9000e123fc1ff07a42(为要查询数据的id)").get({success:res=>{console.log(res)}})或getData(){db.collection("test").doc("").get().then(res=>{this.setData({dataObj:res.data})})},
//用id查询数据库特定一条
getData(){db.collection("test").where({title:"鬼灭之刃"}).get().then(res=>{this.setData({dataObj:res.data})})},
//通过已知信息查询
db.collection("test").get({success:res=>{console.log(res)}}或getData(){db.collection("test").get().then(res=>{this.setData({dataObj:res.data})})//结果给res
//查询数据库所有数据
js文件
data: {dataObj:""},getData(){db.collection("test").doc("b69f67c0626fac9000e123fc1ff07a42").get({success:res=>{console.log(res)this.setData({dataObj:res.data //转给前端})}})
html文件
<button type="primary" bindtap="getData">点击获取数据</button>
<view>{{dataObj.title}}-{{dataObj.author}}</view>//输出
多个数据输出
js文件
data: {dataObj:""},getData(){db.collection("test").get({success:res=>{console.log(res)this.setData({dataObj:res.data //获取所有数据转给前端})}})
html文件
<view wx:for="{{dataObj}}">{{item.title}}-{{item.author}}</view>//输出
添加数据
addData(){db.collection("test").add({data:{要添加的内容}})},
从页面增加数据
js
btnsub(res){var val=res.detail.value;db.collection("test").add({data:val})},html
<form bindsubmit="btnsub">
<input name="title" placeholder="请输入标题"></input>
<input name="author" placeholder="请输入作者"></input>
<textarea name="content" placeholder="请输入内容"></textarea>
<button type="primary" form-type="submit">提交</button>
<button type="primary" form-type="reset">重置</button>
</form>
更新
upData(){db.collection("test").doc("058dfefe626fc38901155a6169db0a17").update({data:{要更新的内容}}).then(res=>{console.log(res)})},upData(){db.collection("test").doc("058dfefe626fc38901155a6169db0a17").ste({data:{要更新的内容}}).then(res=>{console.log(res)})},//set覆盖原来的内容
删除
Delete(){db.collection("test").doc("058dfefe626fc38901155a6169db0a17").remove()},
从输入框获得
var myvau="";
myinp(res){var vau=res.detail.value;myvau=vau},Delete(){db.collection("test").doc(myvau).remove()}, //不用引号
查询个数
js
btnNum(){db.collection("test").count()},html<button type="primary" bindtap="btnNum">查询个数</button>
实时展示更新数据
var myavu="";
myinp(res){var vau=res.detail.value;myvau=vau},
Delete(){db.collection("test").doc(myvau).remove()},
getData(){db.collection("test").get().then(res=>{this.setData({dataArr:res.data})})},onLoad: function (options) {this.getData();db.collection("test").watch({onChange:res=>{this.setData({dataArr:res.docs})},onError:err=>{console.log(err)}})},
其他
getData(){db.collection("test").limit(3).skip(3).field({title:true,author:true}).orderBy("time","desc").get().then(res=>{this.setData({dataArr:res.data})})},filed 要查询哪几项
limit 只要几条数据
skip 跳过几条数据
orderBy 按要求排序
比较符
getData(){db.collection("test").where({hits:_.eq(999)//等于999 其他看api}).get().then(res=>{this.setData({dataList:res.data})})
},