1 數組查詢
1)新建帶雲開發功能的小程序
2)點擊雲開發->數據庫->新建集合->添加幾條記錄
3)編碼
index.wxml
<button class="b1" bindtap="query">數組查詢</button>
index.json
{}
index.wxss
.b1{
background-color: #eee;
}
index.js
/ 初始化數據庫實例 command
const db = wx.cloud.database();
const _ = db.command
Page({
query:function(){
console.log("Query")
//查詢所有內容 獲取並輸出
//db.collection("data").get().then(console.log)
//查詢count爲(in)1 3 4 的數據 不爲是nin
db.collection("data")
.where({
count:_.nin([1,3,4])
})
.get().then(console.log)
}
})
2 字段類型查詢
爲已有數據添加字段,然後
db.collection("data")
//查詢desc字段
.field({
desc:true
})
.get().then(console.log)
3 正則表達式
db.collection("data")
.where({
//new正則對象
name: new db.RegExp({
//正則字符串 匹配name-01 ... name-09
regexp: 'name-0[1-9]',
options: 'i'
})
})
.get().then(console.log)
4 地理位置查詢
index.wxml
<button class="b1" bindtap="add">新增地點</button>
<button class="b1" bindtap="query">字段查詢</button>
index.js
const db = wx.cloud.database();
const _ = db.command
Page({
query: function () {
console.log("Query")
//數據0的讀取維度 ;不用res拿到的是一個數組,所以使用回調函數res方便後續處理
db.collection('location').get().then(res=>{
console.log(res.data[0].location.latitude)
})
},
//添加地理位置索引
add:function(){
db.collection('location').add({
//data表示需新增的json數據
data:{
location:db.Geo.Point(100.0012,10.0022)
}
//then() 異步執行 then前程序執行完後,執行then內部數據(一層一層剝開你的心)
}).then(res=>{
db.collection('location').add({
data:{
location:db.Geo.Point(101.0012,10.0022)
}
}).then(res=>{
db.collection('location').add({
data: {
//創建一個點
location: db.Geo.Point(101.0012, 10.0022)
}
})
})
})
}
})