unicloud8-doc引用于skip_orderBy_limit_field查询条件

48 阅读3分钟

引用、查询条件

之前是读取操作get,现在学的是doc引用

doc获取对该集合中指定id的记录的引用 实际就是根据id查询这条id所在字段的完整数据

以后会经常根据ID查数据

在前端写一个请求方法

<template>
	<view class="home">
    <view class="out">
      <view class="row">姓名:{{}}</view>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			}
		},
		onLoad() {
      // 调用数据库查询方法
      this.getData()
		},
		methods: {
      // 获取数据库中的方法
      getData(){
        uniCloud.callFunction({
          name:"cloudDemoGet"
        }).then(res=>{
          console.log(res);
        })
      }
		}
	}
</script>

<style lang="scss">
.out {
  padding: 30rpx;
  .row {
    border-bottom: 1px solid #ccc;
    padding: 20rpx 0;
  }
}
</style>

新建一个云函数cloudDemoGet,测试doc方法的用法 在数据库中随意复制一条数据的id到doc中,测试能查询出什么东西: index.js

'use strict';
// 连接数据库
const db = uniCloud.database()
exports.main = async (event, context) => {
  // await 等待异步请求拿到结果之后再返回
	let res = await db.collection("users").doc("63d63c1bf5cf3a4b66ecc42d").get();
  return res;
}; 

image.png

上面使用doc方法把数据库中李四这一条数据查出来了 image.png

limit 限制请求数据的条数

使用场景如分页功能

'use strict';
// 连接数据库
const db = uniCloud.database()
exports.main = async (event, context) => {
  // await 等待异步请求拿到结果之后再返回
	let res = await db.collection("users").limit(4).get();
  return res;
}; 

通过limit限制拿到了4条数据 image.png

测试把拿到的结果渲染到页面中

<template>
	<view class="home">
    <view class="out">
      <view class="row" v-for="item in listArr" :key="item._id">姓名:{{item.name}}</view>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
        listArr:[]
			}
		},
		onLoad() {
      // 调用数据库查询方法
      this.getData()
		},
		methods: {
      // 获取数据库中的方法
      getData(){
        uniCloud.callFunction({
          name:"cloudDemoGet"
        }).then(res=>{
          this.listArr=res.result.data
        })
      }
		}
	}
</script>

<style lang="scss">
.out {
  padding: 30rpx;
  .row {
    border-bottom: 1px solid #ccc;
    padding: 20rpx 0;
  }
}
</style>

image.png

limit经常和skip配合使用 skip是跳过指定数量的数据条数,多用于分页

这里skip传值1就是过滤掉第一条数据

'use strict';
// 连接数据库
const db = uniCloud.database()
exports.main = async (event, context) => {
  // await 等待异步请求拿到结果之后再返回
	let res = await db.collection("users").limit(4).skip(1).get();
  return res;
}; 

前面拿到的数据是张三李四小红科比,现在过滤掉第一条之后,变成了李四小红科比麦迪了

如果做分页,每页显示5条数据的话,加入显示第二页的数据,skip传的值就是5了,过滤掉前5条数据 image.png

排序 orderBy

默认排序是根据ID排序,阿里云的id是升序的,腾讯云的id是随机的

默认排序方式是asc顺序排列

'use strict';
// 连接数据库
const db = uniCloud.database()
exports.main = async (event, context) => {
  // await 等待异步请求拿到结果之后再返回
	let res = await db.collection("users").orderBy("_id", "asc").get();
  return res;
}; 

image.png

排序确实是按照数据库的id顺序排列,拿到的数据跟数据库顺序是一样的 image.png

desc 倒序排列

'use strict';
// 连接数据库
const db = uniCloud.database()
exports.main = async (event, context) => {
  // await 等待异步请求拿到结果之后再返回
	let res = await db.collection("users").orderBy("_id", "desc").get();
  return res;
}; 

排序倒过来了 image.png

排序不仅仅是通过id,还可以通过表内的其他字段,例如时间等字段进行升序或降序排列

如果是微信小程序,不能用_id进行排序,因为他的id是随机的,腾讯云的数据库是没有自增的

一条数据有多个字段,之前是把整条数据全部拿过来,但是这样会造成浪费,如果一条数据只需要用到一个字段,例如姓名,但是把这条数据下全部字段一起拿过来,就会对服务器造成压力

可以通过field指定需要返回的字段

这个方法通过对具体的某一个字段设置ture或false,来确定是单独获取这一个字段,还是单独排除这个字段,这个方法不能同时设置获取多个字段,或排除多个字段

它的参数是对象类型

参数类型必填说明
-object过滤字段对象,包含字段名和策略,不返回传fals

注意

  • field内指定是否返回某字段时,不可混用true/false。即{'a': true, 'b': false}是一种错误的参数格式
  • 只有使用{ '_id': false }明确指定不要返回_id时才会不返回_id字段,否则_id字段一定会返回。

测试

'use strict';
// 连接数据库
const db = uniCloud.database()
exports.main = async (event, context) => {
  // await 等待异步请求拿到结果之后再返回
	let res = await db.collection("users").field({"name": true}).get();
  return res;
}; 

在控制台可以看到,现在拿到的值除了name和id之外,没有其他字段 id是唯一标识符,是一定存在的,除非明确说明不带id image.png