uniCloud基礎入門(三)---云數據庫基礎
uniCloud云數據庫基礎
文檔地址
https://uniapp.dcloud.io/uniCloud/hellodb
未經本人允許,禁止轉載
可以看到官方給出了兩種方式
云函數操作數據庫(騰訊云服務空間)
這里我們注意講騰訊云服務空間操作
首先需要創建服務空間
https://unicloud.dcloud.net.cn/login
免費版即可
創建數據表
兩種方式 網頁創建 /api創建
手動創建
可以使用模板創建 這里我們不使用模板
你可以編輯設置表結構 也可設置索引
表結構字段 說明 參考文檔
https://uniapp.dcloud.io/uniCloud/schema
向表中插入一個字段
api創建
const db = uniCloud.database(); db.createCollection("xxxx")
云函數中
本地執行就行 執行效果
操作數據庫
無論是整個數據表 還是單個記錄 都是要經過以下流程
先獲取集合的引用
整個集合的引用
const db = uniCloud.database();
// 獲取 xxx 集合的引用
const collection = db.collection(‘xxx’);
單個記錄的引用
collection.doc(“id”)
https://uniapp.dcloud.io/uniCloud/cf-database?id=%E9%9B%86%E5%90%88
我們以這個數據表為例
目前沒有數據
連接數據庫 獲取數據表引用
const db = uniCloud.database(); // 獲取 `xxx` 集合的引用 const collection = db.collection('xxx');
增加
let res = await collection.add({username:"dmhsq"}) console.log(res)
可以看到增加成功了
統計記錄個數
let res = await collection.count() console.log(res)
獲取全部記錄
let res = await collection.get() console.log(res)
獲取指定id的記錄
比如這里我拿到
id 28ee4e3e602fb13c064475431a7966e7
let res = await collection.doc("28ee4e3e602fb13c064475431a7966e7") console.log(res)
返回proxy代理對象
let res = await collection.doc("28ee4e3e602fb13c064475431a7966e7").get() console.log(res)
增加查詢條件
我們使用where來操作
格式為
collection.where({ username: "匹配的值" 可以使用> < 什么的 })
如果使用指令 則為
const dbCmd = db.command let res = await collection.where({ username:dbCmd.eq("匹配的值") }).get()
文檔 指令表
https://uniapp.dcloud.io/uniCloud/cf-database?id=%e8%ae%b0%e5%bd%95-record-document
比如我們查詢username為dmhsq的記錄
collection.where({userename:"dmhsq"}) 或者 指令方式 const dbCmd = db.command let res = await collection.where({ username:dbCmd.eq("dmhsq") })
let res = await collection.where({userename:"dmhsq"}).get() 或者 const dbCmd = db.command let res = await collection.where({ username:dbCmd.eq("dmhsq") }).get() console.log(res)
分頁
為了方便觀察 這里我把dmhsq刪除 新增了5個數據
這里的num 為跳過指定數量的記錄
如果有5個數據 為0就是獲取5個 為1就是獲取4個
為了方便觀察 在云端運行
collection.skip(num)
獲取數據
我們傳入0
let res = await collection.skip(0).get() console.log(res)
傳入1
let res = await collection.skip(1).get() console.log(res)
num為返回的個數限制 最多為num個
collection.limit(num)
這里我們設置為2
let res = await collection.limit(2).get() console.log(res)
邏輯如下
獲取第二頁 就跳過第一頁的全部
let pages = event.p; //頁數 let nums = event.n; //每頁個數 let res = await collection.skip((pages-1)*nums).limit(nums).get() console.log("當前頁數為"+pages) console.log(res)
我們設定每頁兩個
配置
右鍵點擊云函數目錄
選擇配置運行測試參數 我們傳入 p=1 n=2
// 本文件中的json內容將在云函數【運行】時作為參數傳給云函數。 // 配置教程參考:https://uniapp.dcloud.net.cn/uniCloud/quickstart?id=runparam { "p":1, "n":2 }
這時我們讓p=2
如果我們讓p=1 n=4 或者p=2 n=4
也就是每頁 4個 返回第一頁和第二頁
分頁完成
排序
collection.orderBy("字段名","升序/降序").get() 升序為asc 降序dssc
collection.orderBy("username","asc").get()
collection.orderBy("username","desc").get()
指定需要返回的字段
這里我們只返回_id字段
collection.field({"_id":true})
let res = await collection.field({"_id":true}).get() console.log(res)
在field中
指定某字段不返回 “字段名”:false
指定只返回某字段 “字段名”:true
字段更新指令
參考文檔
https://uniapp.dcloud.io/uniCloud/cf-database?id=%e5%ad%97%e6%ae%b5%e6%9b%b4%e6%96%b0%e6%8c%87%e4%bb%a4-update-command
這里以 username為 我是4為例
collection.where({username:"我是4"})
我們給數據表新增一個字段
使用update
collection.where({username:"我是4"}).update({username:"我是豬"})
updated是更新的條數 這里的updated為1 我們更新了一條數據所以為1
collection.doc('_id字段的值').set({username:"我是4"})
可能是為了防止隨意覆蓋或安全什么的 使用where并不能使用set 所以使用doc獲取
可以打印下 where 和 doc 獲取的對象有什么不同
collection.doc('_id字段的值') collection.where(字段名:'字段的值')
可以發現 這里每xxxx字段了
更多更新指令 參考文檔
https://uniapp.dcloud.io/uniCloud/cf-database?id=%e6%9b%b4%e6%96%b0%e6%96%87%e6%a1%a3
來刪除
collection.where({xxxx:5}).remove()
這里的deleted類似于 updated 是刪除的個數
這里已經刪除成功
前端直接操作
和云函數操作沒有多少區別 只是 需要放通權限
我們只舉幾個例子 其他的可參考云函數寫法
或者文檔
https://uniapp.dcloud.io/uniCloud/clientdb?id=jssdk
放開權限
放通操作權限 可根據需求 這里我全部放開
獲取數據
const db = uniCloud.database() let res = db.collection('xxx').get().then(res => { console.log(res) })
增加
const db = uniCloud.database() let res = db.collection('xxx').add({ username: "我是dmhsq" }).then(res => { console.log(res) })
統計個數
const db = uniCloud.database() let res = db.collection('xxx').count().then(res => { console.log(res) })
更新
const db = uniCloud.database() let res = db.collection('xxx').where({ username: "我是1" }).update({ username: "小可愛1" }).then(res => { console.log(res) })
刪除
const db = uniCloud.database() let res = db.collection('xxx').where({ username: "小可愛1" }).remove().then(res => { console.log(res) })
其它請參考上面的云函數寫法
感謝您的閱讀
大學之道亦在自身,努力學習,熱血青春
SQL 云數據庫 MySQL 數據庫
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。