uniCloud云資料庫基礎
云開發基礎
https://blog.csdn.net/qq_42027681/category_10802794.html
檔案地址
https://uniapp.dcloud.io/uniCloud/hellodb
未經本人允許,禁止轉載

云資料庫基礎
- 云函式操作資料庫(騰訊云服務空間)
- 創建資料表
- 手動創建
- api創建
- 操作資料庫
- 連接資料庫 獲取資料表參考
- 增加
- 統計記錄個數
- 獲取全部記錄
- 獲取指定id的記錄
- 參考指定記錄(可進行 獲取 更新 等操作)
- 獲取
- 增加查詢條件
- 獲取
- 分頁
- 跳過指定數量記錄數
- 回傳的結果集(檔案數量)的限制
- 實作分頁
- 排序
- 升序
- 降序
- 指定需要回傳的欄位
- 欄位更新指令
- 獲取指定記錄參考
- 設定欄位值
- 只更新
- 更新如果沒有就新建(覆寫式)
- 洗掉
- 前端直接操作
- 放開權限
- 獲取資料
- 增加
- 統計個數
- 更新
- 洗掉
- 其它請參考上面的云函式寫法
可以看到官方給出了兩種方式
云函式操作資料庫(騰訊云服務空間)
這里我們注意講騰訊云服務空間操作
首先需要創建服務空間
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-collection](https://uniapp.dcloud.io/uniCloud/cf-database?id=%E9%9B%86%E5%90%88-collection)
我們以這個資料表為例
目前沒用資料

連接資料庫 獲取資料表參考
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)
})



其它請參考上面的云函式寫法
感謝您的閱讀
大學之道亦在自身,努力學習,熱血青春
如果對編程感興趣可以加入我們的qq群一起交流:974178910
??大家好,我是代碼哈士奇,是一名軟體學院網路工程的學生,因為我是“狗”,狗走千里吃肉,想把大學期間學的東西和大家分享,和大家一起進步,但由于水平有限,博客中難免會有一些錯誤出現,有紕漏之處懇請各位大佬不吝賜教!,博客主頁:https://dmhsq.blog.csdn.net/,
騰訊云社區專欄 https://cloud.tencent.com/developer/user/5264655

未經本人允許,禁止轉載
有問題可以下方留言,看到了會回復哦
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/261358.html
標籤:其他
上一篇:2021 Java后端學習路線

