柚子快報(bào)邀請(qǐng)碼778899分享:微信小程序連接云數(shù)據(jù)庫基本使用
柚子快報(bào)邀請(qǐng)碼778899分享:微信小程序連接云數(shù)據(jù)庫基本使用
微信小程序連接云數(shù)據(jù)庫基本使用
demo_list數(shù)據(jù)庫
這里僅僅展示了基本使用和常用函數(shù),微信官方文檔鏈接: 微信小程序云數(shù)據(jù)庫官方文檔
一、連接數(shù)據(jù)庫
const db=wx.cloud.database()
二、操作數(shù)據(jù)庫
1.get 函數(shù)
獲取 demo_list 數(shù)據(jù)庫數(shù)據(jù)
db.collection("demo_list").get({
success: res=>{
console.log(res)
}
})
2. doc 函數(shù)
按照 doc 的條件獲取 demo_list 數(shù)據(jù)庫數(shù)據(jù)
db.collection("demo_list").doc("f18e14fa652e8708032637034ac9ef78").get({
success: res=>{
console.log(res)
}
})
3.add 函數(shù)
用 add 函數(shù)添加數(shù)據(jù),同時(shí)使用 promise 回調(diào)測試添加的數(shù)據(jù)
addData(){
// 數(shù)據(jù)加載中...
wx.showLoading({
title: '數(shù)據(jù)加載中...',
mask: true
})
// 添加元素
db.collection("demo_list").add({
data:{
user_name: "唐平",
address: "上海市嘉定區(qū)",
mobile: "135956252456"
}
}).then(res=>{
console.log(res)
})
4、update 函數(shù)
db.collection("demo_list").doc("41d77edc652e93b407906cd65d1c4e56").update({
data:{
// 如果字段存在則更改,不存在則新增字段
user_name: "王五"
}
}).then(res=>{
console.log(res)
})
按照條件查詢更新(updated為1更新成功)
{
"stats":{
"updated":1
},
"errMsg":"document.update:ok"
}
5、set 函數(shù)
按照條件覆蓋之前的屬性
db.collection("demo_list").doc("41d77edc652e93b407906cd65d1c4e56").set({
data:{
user_name: "王六",
postTime: "2020-10-10"
}
}).then(res=>{
console.log(JSON.stringify(res))
})
6、del 刪除
db.collection("demo_list").doc("a5782af7652e93e8000333250819c3ca")
.remove()
.then(res=>{
console.log(res)
})
按照條件刪除(removed為1刪除成功)
{
"stats":{
"removed":1
},
"errMsg":"document.remove:ok"
}
7、count 獲取個(gè)數(shù)
db.collection("demo_list").count().then(res=>{
console.log(res)
})
8、watch監(jiān)聽(必須要有 onChange 與 onError 進(jìn)行回調(diào))
db.collection("demo_list").watch({
onChange:res=>{
console.log(res)
},
onError:err=>{
console.log(err)
}
})
測試刪除:
刪除前輸出為( 刪除 id 為 41d77edc652e93b407906cd65d1c4e56 的記錄 ):
{
"id": 0,
"docChanges": [{
"id": 0,
"dataType": "init",
"queueType": "init",
"docId": "f18e14fa652e8708032637034ac9ef78",
"doc": {
"_id": "f18e14fa652e8708032637034ac9ef78",
"address": "福建省 漳州市 龍文區(qū)",
"mobile": "13596288733",
"user_name": "唐平"
}
}, {
"id": 0,
"dataType": "init",
"queueType": "init",
"docId": "a5782af7652e87eb00025cfa6abf49bb",
"doc": {
"_id": "a5782af7652e87eb00025cfa6abf49bb",
"address": "澳門特別行政區(qū) 離島",
"mobile": "18943772748",
"user_name": "易超"
}
}, {
"id": 0,
"dataType": "init",
"queueType": "init",
"docId": "8de6ebcc652e882d079b2a4d019657ed",
"doc": {
"_id": "8de6ebcc652e882d079b2a4d019657ed",
"address": "云南省 西雙版納傣族自治州 其它區(qū)",
"mobile": "13588962266",
"user_name": "段濤"
}
}, {
"id": 0,
"dataType": "init",
"queueType": "init",
"docId": "41d77edc652e93b407906cd65d1c4e56",
"doc": {
"_id": "41d77edc652e93b407906cd65d1c4e56",
"_openid": "oJId45dfdgfdgdggsdfg0ZEVas",
"address": "上海市嘉定區(qū)",
"mobile": "135956252456",
"postTime": "2020-10-10",
"user_name": "王六"
}
}, {
"id": 0,
"dataType": "init",
"queueType": "init",
"docId": "7dc1d502652e93bf07972139040bd3fa",
"doc": {
"_id": "7dc1d502652e93bf07972139040bd3fa",
"_openid": "oJId45dfdgfdgdggsdfg0ZEVas",
"address": "上海市嘉定區(qū)",
"mobile": "135956252456",
"user_name": "唐平"
}
}],
"docs": [{
"_id": "f18e14fa652e8708032637034ac9ef78",
"address": "福建省 漳州市 龍文區(qū)",
"mobile": "13596288733",
"user_name": "唐平"
}, {
"_id": "a5782af7652e87eb00025cfa6abf49bb",
"address": "澳門特別行政區(qū) 離島",
"mobile": "18943772748",
"user_name": "易超"
}, {
"_id": "8de6ebcc652e882d079b2a4d019657ed",
"address": "云南省 西雙版納傣族自治州 其它區(qū)",
"mobile": "13588962266",
"user_name": "段濤"
}, {
"_id": "41d77edc652e93b407906cd65d1c4e56",
"_openid": "oJId45dfdgfdgdggsdfg0ZEVas",
"address": "上海市嘉定區(qū)",
"mobile": "135956252456",
"postTime": "2020-10-10",
"user_name": "王六"
}, {
"_id": "7dc1d502652e93bf07972139040bd3fa",
"_openid": "oJId45dfdgfdgdggsdfg0ZEVas",
"address": "上海市嘉定區(qū)",
"mobile": "135956252456",
"user_name": "唐平"
}],
"type": "init",
"requestId": "1697879416647_0.9092538129463852",
"watchId": "watchid_1697879416228_0.7892680514541097"
}
刪除后輸出為:
{
"id": 1,
"docChanges": [{
"id": 1,
"dataType": "remove",
"queueType": "dequeue",
"docId": "7dc1d502652e93bf07972139040bd3fa",
"doc": {
"_id": "7dc1d502652e93bf07972139040bd3fa",
"_openid": "oJId45dfdgfdgdggsdfg0ZEVas",
"address": "上海市嘉定區(qū)",
"mobile": "135956252456",
"user_name": "唐平"
}
}],
"docs": [{
"_id": "f18e14fa652e8708032637034ac9ef78",
"address": "福建省 漳州市 龍文區(qū)",
"mobile": "13596288733",
"user_name": "唐平"
}, {
"_id": "a5782af7652e87eb00025cfa6abf49bb",
"address": "澳門特別行政區(qū) 離島",
"mobile": "18943772748",
"user_name": "易超"
}, {
"_id": "8de6ebcc652e882d079b2a4d019657ed",
"address": "云南省 西雙版納傣族自治州 其它區(qū)",
"mobile": "13588962266",
"user_name": "段濤"
}, {
"_id": "41d77edc652e93b407906cd65d1c4e56",
"_openid": "oJId45dfdgfdgdggsdfg0ZEVas",
"address": "上海市嘉定區(qū)",
"mobile": "135956252456",
"postTime": "2020-10-10",
"user_name": "王六"
}],
"requestId": "1697879416647_0.9092538129463852",
"watchId": "watchid_1697879416228_0.7892680514541097"
}
刪除:
{
"stats": {
"removed": 1
},
"errMsg": "document.remove:ok"
}
9、limit 限制查詢
// 限制一次性查詢3個(gè)
db.collection("demo_list").limit(3)
.get().then(res=>{
console.log(res)
})
10、orderBy 排序
// 根據(jù) time 升序排序(升序:asc、降序:desc)
db.collection("demo_list").orderBy("time","asc").limit(3)
.get().then(res=>{
console.log(JSON.stringify(res))
})
{
"data": [
{
"_id": "f18e14fa652e8708032637034ac9ef78",
"address": "福建省 漳州市 龍文區(qū)",
"mobile": "13596288733",
"time": "2023-10-21T15:05:52.462Z",
"user_name": "唐平"
},
{
"_id": "a5782af7652e87eb00025cfa6abf49bb",
"address": "澳門特別行政區(qū) 離島",
"mobile": "18943772748",
"time": "2023-10-21T15:06:34.244Z",
"user_name": "易超"
},
{
"_id": "8de6ebcc652e882d079b2a4d019657ed",
"address": "云南省 西雙版納傣族自治州 其它區(qū)",
"mobile": "13588962266",
"time": "2023-10-21T15:06:49.219Z",
"user_name": "段濤"
}
],
"errMsg": "collection.get:ok"
}
11、skip函數(shù)
// skip跳過三條(如果limit限制三條,skip相當(dāng)于查詢第二頁)
db.collection("demo_list").orderBy("time","asc").limit(3).skip(3)
.get().then(res=>{
console.log(JSON.stringify(res))
})
12、field 函數(shù)
// field 接收我們想要的數(shù)據(jù),user_name與address為true表示接收
db.collection("demo_list").orderBy("time","asc").limit(3).skip(0)
.field({
user_name: true,
address: true
})
.get().then(res=>{
console.log(JSON.stringify(res))
})
輸出結(jié)果:
{
"data": [
{
"_id": "f18e14fa652e8708032637034ac9ef78",
"address": "福建省 漳州市 龍文區(qū)",
"user_name": "唐平"
},
{
"_id": "a5782af7652e87eb00025cfa6abf49bb",
"address": "澳門特別行政區(qū) 離島",
"user_name": "易超"
},
{
"_id": "8de6ebcc652e882d079b2a4d019657ed",
"address": "云南省 西雙版納傣族自治州 其它區(qū)",
"user_name": "段濤"
}
],
"errMsg": "collection.get:ok"
}
三、Command
1、command
官方建議這樣定義 command:
const db=wx.cloud.database()
const _ = db.command
2、where條件查詢
// 查詢地址為“福建省 漳州市 龍文區(qū)”的用戶
db.collection("demo_list")
.where({
address: "福建省 漳州市 龍文區(qū)"
})
.get()
.then(res=>{
console.log(JSON.stringify(res));
this.setData({
dataList: res.data
})
})
控制臺(tái)輸出結(jié)果:
{
"data": [{
"_id": "f18e14fa652e8708032637034ac9ef78",
"address": "福建省 漳州市 龍文區(qū)",
"mobile": "13596288733",
"time": "2023-10-21T15:05:52.462Z",
"user_name": "唐平"
}],
"errMsg": "collection.get:ok"
}
3、eq 相等
// 查詢地址為“福建省 漳州市 龍文區(qū)”的用戶
db.collection("demo_list")
.where({
// 等價(jià)于address: "福建省 漳州市 龍文區(qū)"
// _ 是開頭定義的 “_ = db.command”
address: _.eq("福建省 漳州市 龍文區(qū)")
})
.get()
.then(res=>{
console.log(JSON.stringify(res));
this.setData({
dataList: res.data
})
})
4、neq 不相等
db.collection("demo_list")
.where({
// 查詢地址不為 “福建省 漳州市 龍文區(qū)”
address: _.neq("福建省 漳州市 龍文區(qū)")
})
.get()
.then(res=>{
console.log(JSON.stringify(res));
this.setData({
dataList: res.data
})
控制臺(tái)輸出:
{
"data": [{
"_id": "a5782af7652e87eb00025cfa6abf49bb",
"address": "澳門特別行政區(qū) 離島",
"mobile": "18943772748",
"time": "2023-10-21T15:06:34.244Z",
"user_name": "易超"
}, {
"_id": "8de6ebcc652e882d079b2a4d019657ed",
"address": "云南省 西雙版納傣族自治州 其它區(qū)",
"mobile": "13588962266",
"time": "2023-10-21T15:06:49.219Z",
"user_name": "段濤"
}, {
"_id": "41d77edc652e93b407906cd65d1c4e56",
"_openid": "oJId45dfdgfdgdggsdfg0ZEVas",
"address": "上海市嘉定區(qū)",
"mobile": "135956252456",
"postTime": "2020-10-10",
"time": "2023-10-21T15:07:12.707Z",
"user_name": "王六"
}],
"errMsg": "collection.get:ok"
}
5、lte 小于等于
.where({
// 查詢xxx小于等于5個(gè)
xxx: _.lte(5)
})
6、lt 小于
.where({
// 查詢xxx小于5個(gè)
xxx: _.lt(5)
})
7、gte 大于等于
.where({
// 查詢xxx大于等于5個(gè)
xxx: _.gte(5)
})
8、gt 大于
.where({
// 查詢xxx大于5個(gè)
xxx: _.gt(5)
})
9、in 包含
.where({
// 參數(shù)是列表,查詢包含澳門和云南省的信息
address: _.in(["澳門特別行政區(qū) 離島","云南省 西雙版納傣族自治州 其它區(qū)"])
})
控制臺(tái)輸出:
{
"data": [{
"_id": "a5782af7652e87eb00025cfa6abf49bb",
"address": "澳門特別行政區(qū) 離島",
"mobile": "18943772748",
"time": "2023-10-21T15:06:34.244Z",
"user_name": "易超"
}, {
"_id": "8de6ebcc652e882d079b2a4d019657ed",
"address": "云南省 西雙版納傣族自治州 其它區(qū)",
"mobile": "13588962266",
"time": "2023-10-21T15:06:49.219Z",
"user_name": "段濤"
}],
"errMsg": "collection.get:ok"
}
10、nin 不包含
.where({
// 參數(shù)是列表,查詢不包含澳門和云南省的信息(黑名單)
address: _.nin(["澳門特別行政區(qū) 離島","云南省 西雙版納傣族自治州 其它區(qū)"])
})
控制臺(tái)輸出:
{
"data": [{
"_id": "f18e14fa652e8708032637034ac9ef78",
"address": "福建省 漳州市 龍文區(qū)",
"mobile": "13596288733",
"time": "2023-10-21T15:05:52.462Z",
"user_name": "唐平"
}, {
"_id": "41d77edc652e93b407906cd65d1c4e56",
"_openid": "oJId45dfdgfdgdggsdfg0ZEVas",
"address": "上海市嘉定區(qū)",
"mobile": "135956252456",
"postTime": "2020-10-10",
"time": "2023-10-21T15:07:12.707Z",
"user_name": "王六"
}],
"errMsg": "collection.get:ok"
}
11、and 多個(gè)條件同時(shí)滿足
.where({
// 查詢 hits 在 100-400 之間
hits:_.and(_.gt(100),_.lte(400))
})
12、 or 滿足其中一個(gè)條件即可
.where({
// 查詢 hits 等于 235 或 等于 222
hits:_.or(_.eq(235),_.eq(222))
})
13、where查詢多個(gè)字段
如果使用 _.or() 和 _.and() 只能針對(duì)一個(gè)字段
db.collection("demo_list")
.where()
.get()
.then(res=>{
console.log(JSON.stringify(res));
this.setData({
dataList: res.data
})
})
(1) 用戶 叫 “唐平” 或 hits 小于 500
.where(_.or([
{
hits: _.lt(500)
},
{
user_name: _.eq("唐平")
}
]))
(2) 用戶 叫 “唐平” 且 hits 大于 500
.where(_.and([
{
hits: _.gt(500)
},
{
user_name: _.eq("唐平")
}
]))
14、exists 查詢有這個(gè)字段的數(shù)據(jù)
.where({
time: _.exists(true)
})
返回有 time 字段的數(shù)據(jù)
15、mod 取余數(shù)學(xué)運(yùn)算
用的不多,略
16、size 對(duì)字段中的數(shù)組進(jìn)行操作
// 返回有兩個(gè)數(shù)組長度的tabs的數(shù)據(jù)
.where({
tabs: _.size(2)
})
17、all 同時(shí)包含幾個(gè)條件
// 返回tabs為 ['數(shù)碼','科技'] 的數(shù)據(jù)
.where({
tabs: _.all(['數(shù)碼','科技'])
})
18、elemMatch 數(shù)組字段的查詢篩選條件
要求數(shù)組中包含至少一個(gè)滿足 elemMatch 給定的所有條件的元素
// 找出 places 數(shù)組字段中至少同時(shí)包含一個(gè)滿足 “area 大于 100 且 age 小于 2” 的元素
.where({
places: _.elemMatch({
area: _.gt(100),
age: _.lt(2),
})
})
19、inc 增加(參數(shù)為負(fù)數(shù)時(shí)也可以用來減少)
Number類型數(shù)據(jù)才可以
(1) 增加 hits
// hits 增加 5
db.collection("demo_list").doc("41d77edc652e93b407906cd65d1c4e56")
.update({
data:{
hits: _.inc(5)
}
})
.then(res=>{
console.log(JSON.stringify(res));
this.setData({
dataList: res.data
})
})
(2) 減少 hits
// hits 減少 5
db.collection("demo_list").doc("41d77edc652e93b407906cd65d1c4e56")
.update({
data:{
hits: _.inc(-5)
}
})
.then(res=>{
console.log(JSON.stringify(res));
this.setData({
dataList: res.data
})
})
20、刪除某個(gè)字段
// 刪除 id 對(duì)應(yīng)的 postTime 字段
db.collection("demo_list").doc("41d77edc652e93b407906cd65d1c4e56")
.update({
data:{
postTime: _.remove()
}
})
.then(res=>{
console.log(JSON.stringify(res));
this.setData({
dataList: res.data
})
})
21、set 把原來的字段對(duì)象給覆蓋掉
db.collection("demo_list").doc("41d77edc652e93b407906cd65d1c4e56")
.update({
data:{
style: _.set({
back: "pink"
})
}
})
22、min、max比大小…
略
23、push 對(duì)數(shù)組添加
(1) 不寫 Number 默認(rèn)在最后面添加
.updata({
data:{
// push 對(duì)數(shù)組字段后面添加"aaa"和"bbb"
list: _.push(['aaa'],['bbb'])
}
})
(2) 指定追加位置
.updata({
data:{
// push 對(duì)數(shù)組字段后面添加"aaa"和"bbb"
list: _.push({
each:['新視覺','實(shí)訓(xùn)'],
// 下標(biāo)從0開始
position: 1
})
}
})
24、pop 刪除數(shù)組最后一個(gè)元素
.updata({
data:{
// 刪除數(shù)組最后的元素
list: _.pop()
}
})
25、unshift 對(duì)數(shù)組字段后面添加
.updata({
data:{
// 在數(shù)組開頭添加多個(gè)元素
tabs:_.unshift(['智能','新聞'])
}
})
26、shift 刪除數(shù)組第一個(gè)元素
.updata({
data:{
// 在數(shù)組開頭添加多個(gè)元素
tabs:_.shift()
}
})
27、pull 移除數(shù)組中匹配的元素
.updata({
data:{
// 移除數(shù)組中的“數(shù)碼”
tabs:_.pull('數(shù)碼')
}
})
柚子快報(bào)邀請(qǐng)碼778899分享:微信小程序連接云數(shù)據(jù)庫基本使用
文章鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。