uniCloud基础入门(三)---云数据库基础

网友投稿 1331 2022-05-29

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

uniCloud基础入门(三)---云数据库基础

如果我们让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小时内删除侵权内容。

上一篇:python多线程详解
下一篇:【精选单品】基层医院也能享受三甲资源,智慧医疗系统,真正解决看病难问题
相关文章