unicloud where 使用

2024-03-08 23:04
文章标签 使用 unicloud

本文主要是介绍unicloud where 使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

where介绍

在uniCloud中,WHERE是一个用于指定查询条件的关键字。它允许用户根据特定的条件来筛选和查询云数据库中的数据。WHERE语句的基本语法格式是WHERE condition,其中condition表示查询条件,可以是一个或多个逻辑表达式组成的条件。

在uniCloud的查询操作中,WHERE子句的使用非常灵活,可以支持多种查询条件,包括等于、不等于、大于、小于、范围查询等。同时,它还可以支持多个条件的组合查询,通过逻辑运算符(如AND、OR)将多个条件连接起来,实现更复杂的查询需求。

此外,WHERE子句还支持正则表达式查询,可以使用正则表达式来匹配和筛选符合特定模式的数据。这使得在处理文本数据时,能够更加精确地定位到所需的信息。

WHERE在uniCloud中是一个强大的查询工具,能够帮助用户快速、准确地获取所需的数据。无论是简单的条件查询还是复杂的组合查询,都可以通过WHERE子句来实现。

要查看本文章,请先阅读unicloud 集合 Collection 详解及其使用示例

示例教程如下

where 简单使用(查询对象)

使用where查询时,如果传入的参数是一个对象,将按照字段的值进行相等匹配,包含字段顺序。

如查看users表内年龄是38岁的人

示例代码如下

云函数代码

'use strict';
exports.main = async (event, context) => {const result = await uniCloud.database().collection('users').where({age:38}).get()//返回数据给客户端return result
};

页面引用代码

const where = async _=>{const result = await uniCloud.callFunction({name:'where'})console.log(result)
}

输出结果如下
可以看到,将age为38的都查询出来了

在这里插入图片描述

where 高级使用(查询指令)

查询指令以dbCmd.开头,包括等于、不等于、大于、大于等于、小于、小于等于、in、nin、and、or。

tips:

  • 以下指令挂载在 db.command 下

指令图表如下

指令描述说明
eq等于name:dbCmd.eq(‘qayrup’)
neq不等于删除字段
gt大于加一个数值,原子自增
gte大于等于乘一个数值,原子自乘
it小于数组类型字段追加尾元素,支持数组
ite小于等于数组类型字段删除尾元素,支持数组
in在数组中数组类型字段删除头元素,支持数组
nin不再数组中数组类型字段追加头元素,支持数组
and数组类型字段追加头元素,支持数组
or数组类型字段追加头元素,支持数组

eq 等于

表示字段等于某个值。eq 指令接受一个字面量 (literal),可以是 number, boolean, string, object, array。

事实上在uniCloud的数据库中,等于有两种写法。

比如筛选名字为qayrup的

第一种写法使用对象

云函数示例代码

'use strict';
exports.main = async (event, context) => {const result = await uniCloud.database().collection('users').where({name:'qayrup'}).get()//返回数据给客户端return result
};

输出结果
在这里插入图片描述

第二种写法使用eq指令

云函数代码如下

'use strict';
exports.main = async (event, context) => {// 查询字段等于某个值 第一种写法// const result = await uniCloud.database().collection('users').where({name:'qayrup'}).get()// 查询字段等于某个值 第二个写法 使用eqconst result = await uniCloud.database().collection().where({name:uniCloud.database().command.eq('qayrup')})return result
};

输出结果如下
在这里插入图片描述

neq 不等于

字段不等于。neq 指令接受一个字面量 (literal),可以是 number, boolean, string, object, array。

例如筛选出age不为38的数据

示例代码如下

'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {// 查询字段等于某个值 第一种写法// const result = await uniCloud.database().collection('users').where({name:'qayrup'}).get()// 查询字段等于某个值 第二个写法 使用eq// const result = await uniCloud.database().collection('users').where({// 	name:uniCloud.database().command.eq('qayrup')// }).get()// 查询 age 不等于38的const result = await db.collection('users').where({age:dbCmd.neq(38)}).get()return result
};

输出结果如下

在这里插入图片描述

gt 大于

字段大于指定值。

如筛选年龄大于20的

示例代码如下

'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {// 查询字段等于某个值 第一种写法// const result = await uniCloud.database().collection('users').where({name:'qayrup'}).get()// 查询字段等于某个值 第二个写法 使用eq// const result = await uniCloud.database().collection('users').where({// 	name:uniCloud.database().command.eq('qayrup')// }).get()// 查询 age 不等于38的// const result = await db.collection('users').where({// 	age:dbCmd.neq(38)// }).get()const result = await db.collection('users').where({age:dbCmd.gt(20)}).get()return result
};

输出结果如下
在这里插入图片描述

gte 大于等于

字段大于或等于指定值。

筛选年龄大于等于18的

示例代码如下

'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {// 查询字段等于某个值 第一种写法// const result = await uniCloud.database().collection('users').where({name:'qayrup'}).get()// 查询字段等于某个值 第二个写法 使用eq// const result = await uniCloud.database().collection('users').where({// 	name:uniCloud.database().command.eq('qayrup')// }).get()// 查询 age 不等于38的// const result = await db.collection('users').where({// 	age:dbCmd.neq(38)// }).get()// 筛选年龄大于20的// const result = await db.collection('users').where({// 	age:dbCmd.gt(20)// }).get()// 筛选年龄大于等于18的const result = await db.collection('users').where({age:dbCmd.gte(18)}).get()return result
};

输出结果如下
在这里插入图片描述

lt 小于

字段小于指定值。

筛选年龄小于20的

示例代码如下

'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {// 查询字段等于某个值 第一种写法// const result = await uniCloud.database().collection('users').where({name:'qayrup'}).get()// 查询字段等于某个值 第二个写法 使用eq// const result = await uniCloud.database().collection('users').where({// 	name:uniCloud.database().command.eq('qayrup')// }).get()// 查询 age 不等于38的// const result = await db.collection('users').where({// 	age:dbCmd.neq(38)// }).get()// 筛选年龄大于20的// const result = await db.collection('users').where({// 	age:dbCmd.gt(20)// }).get()// 筛选年龄大于等于18的// const result = await db.collection('users').where({// 	age:dbCmd.gte(18)// }).get()// 筛选年龄小于20的const result = await db.collection('users').where({age:dbCmd.lt(20)}).get()return result
};

输出结果如下
在这里插入图片描述

lte 小于等于

字段小于或等于指定值。

筛选小于等于18的

示例代码如下

'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {// 查询字段等于某个值 第一种写法// const result = await uniCloud.database().collection('users').where({name:'qayrup'}).get()// 查询字段等于某个值 第二个写法 使用eq// const result = await uniCloud.database().collection('users').where({// 	name:uniCloud.database().command.eq('qayrup')// }).get()// 查询 age 不等于38的// const result = await db.collection('users').where({// 	age:dbCmd.neq(38)// }).get()// 筛选年龄大于20的// const result = await db.collection('users').where({// 	age:dbCmd.gt(20)// }).get()// 筛选年龄大于等于18的// const result = await db.collection('users').where({// 	age:dbCmd.gte(18)// }).get()// 筛选年龄小于20的// const result = await db.collection('users').where({// 	age:dbCmd.lt(20)// }).get()// 筛选年龄小于等于十八的const result = await db.collection('users').where({age:dbCmd.lte(18)}).get()return result
};

输出结果如下
在这里插入图片描述

in 在数组中

字段值在给定的数组中。

'use strict';const { setUncaughtExceptionCaptureCallback } = require("process");const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {// 查询字段等于某个值 第一种写法// const result = await uniCloud.database().collection('users').where({name:'qayrup'}).get()// 查询字段等于某个值 第二个写法 使用eq// const result = await uniCloud.database().collection('users').where({// 	name:uniCloud.database().command.eq('qayrup')// }).get()// 查询 age 不等于38的// const result = await db.collection('users').where({// 	age:dbCmd.neq(38)// }).get()// 筛选年龄大于20的// const result = await db.collection('users').where({// 	age:dbCmd.gt(20)// }).get()// 筛选年龄大于等于18的// const result = await db.collection('users').where({// 	age:dbCmd.gte(18)// }).get()// 筛选年龄小于20的// const result = await db.collection('users').where({// 	age:dbCmd.lt(20)// }).get()// 筛选年龄小于等于十八的// const result = await db.collection('users').where({// 	age:dbCmd.lte(18)// }).get()// return result// 名字在['qayrup','张三','ggbond'] 内const result = await db.collection('users').where({name:dbCmd.in(['qayrup','张三','ggbond'] )}).get()return result
};

输出结果如下

在这里插入图片描述

nin 不在数组中

字段值不在给定的数组中。

名字不在[‘qayrup’,‘张三’,‘ggbond’] 内
示例代码如下

'use strict';const { setUncaughtExceptionCaptureCallback } = require("process");const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {// 查询字段等于某个值 第一种写法// const result = await uniCloud.database().collection('users').where({name:'qayrup'}).get()// 查询字段等于某个值 第二个写法 使用eq// const result = await uniCloud.database().collection('users').where({// 	name:uniCloud.database().command.eq('qayrup')// }).get()// 查询 age 不等于38的// const result = await db.collection('users').where({// 	age:dbCmd.neq(38)// }).get()// 筛选年龄大于20的// const result = await db.collection('users').where({// 	age:dbCmd.gt(20)// }).get()// 筛选年龄大于等于18的// const result = await db.collection('users').where({// 	age:dbCmd.gte(18)// }).get()// 筛选年龄小于20的// const result = await db.collection('users').where({// 	age:dbCmd.lt(20)// }).get()// 筛选年龄小于等于十八的// const result = await db.collection('users').where({// 	age:dbCmd.lte(18)// }).get()// return result// 名字在['qayrup','张三','ggbond'] 内// const result = await db.collection('users').where({// 	name:dbCmd.in(['qayrup','张三','ggbond'] )// }).get()// 名字不在['qayrup','张三','ggbond'] 内const result = await db.collection('users').where({name:dbCmd.nin(['qayrup','张三','ggbond'] )}).get()return result
};

输出结果如下
在这里插入图片描述

and 且

表示需同时满足指定的两个或以上的条件。

筛选出 年龄大于20且小于50的

示例代码如下

'use strict';const { setUncaughtExceptionCaptureCallback } = require("process");const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {// 查询字段等于某个值 第一种写法// const result = await uniCloud.database().collection('users').where({name:'qayrup'}).get()// 查询字段等于某个值 第二个写法 使用eq// const result = await uniCloud.database().collection('users').where({// 	name:uniCloud.database().command.eq('qayrup')// }).get()// 查询 age 不等于38的// const result = await db.collection('users').where({// 	age:dbCmd.neq(38)// }).get()// 筛选年龄大于20的// const result = await db.collection('users').where({// 	age:dbCmd.gt(20)// }).get()// 筛选年龄大于等于18的// const result = await db.collection('users').where({// 	age:dbCmd.gte(18)// }).get()// 筛选年龄小于20的// const result = await db.collection('users').where({// 	age:dbCmd.lt(20)// }).get()// 筛选年龄小于等于十八的// const result = await db.collection('users').where({// 	age:dbCmd.lte(18)// }).get()// return result// 名字在['qayrup','张三','ggbond'] 内// const result = await db.collection('users').where({// 	name:dbCmd.in(['qayrup','张三','ggbond'] )// }).get()// 名字不在['qayrup','张三','ggbond'] 内// const result = await db.collection('users').where({// 	name:dbCmd.nin(['qayrup','张三','ggbond'] )// }).get()// 筛选出 年龄大于20且小于50的const result = await db.collection('users').where({age:dbCmd.gt(20).and(dbCmd.lt(50))}).get()return result
};

结果如下
在这里插入图片描述

or 或

表示需满足所有指定条件中的至少一个。

筛选 年龄等于18或年龄等38的

示例代码如下

'use strict';const { setUncaughtExceptionCaptureCallback } = require("process");const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {// 查询字段等于某个值 第一种写法// const result = await uniCloud.database().collection('users').where({name:'qayrup'}).get()// 查询字段等于某个值 第二个写法 使用eq// const result = await uniCloud.database().collection('users').where({// 	name:uniCloud.database().command.eq('qayrup')// }).get()// 查询 age 不等于38的// const result = await db.collection('users').where({// 	age:dbCmd.neq(38)// }).get()// 筛选年龄大于20的// const result = await db.collection('users').where({// 	age:dbCmd.gt(20)// }).get()// 筛选年龄大于等于18的// const result = await db.collection('users').where({// 	age:dbCmd.gte(18)// }).get()// 筛选年龄小于20的// const result = await db.collection('users').where({// 	age:dbCmd.lt(20)// }).get()// 筛选年龄小于等于十八的// const result = await db.collection('users').where({// 	age:dbCmd.lte(18)// }).get()// return result// 名字在['qayrup','张三','ggbond'] 内// const result = await db.collection('users').where({// 	name:dbCmd.in(['qayrup','张三','ggbond'] )// }).get()// 名字不在['qayrup','张三','ggbond'] 内// const result = await db.collection('users').where({// 	name:dbCmd.nin(['qayrup','张三','ggbond'] )// }).get()// 筛选出 年龄大于20且小于50的// const result = await db.collection('users').where({// 	age:dbCmd.gt(20).and(dbCmd.lt(50))// }).get()// 筛选 年龄等于18或年龄等38的const result = await db.collection('users').where({age:dbCmd.eq(18).or(dbCmd.eq(38))}).get()return result
};

输出结果如下
在这里插入图片描述

正则表达式查询

db.RegExp
根据正则表达式进行筛选

正则表达式忘得差不多了,这里就不做示例了

以上指令所示例的数据集

[{"_id":"65ea848410def2d7fe8efb6a","age":18,"email":"qayrup@aliyun.com","intro":"长得像吴彦祖","name":"qayrup"},
{"_id":"65eaa7175e058d32ec579fe7","age":38,"email":"zhangshan@email.com","intro":"一个普普通通的律师","name":"张三","uniIdToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI2NTg4M2Y3MTZlNWQyZDcxODc5MDAxYjEiLCJyb2xlIjpbXSwicGVybWlzc2lvbiI6W10sInVuaUlkVmVyc2lvbiI6IjEuMC4xNiIsImlhdCI6MTcwMzQyNzk0NCwiZXhwIjoxNzAzNDM1MTQ0fQ.hy40ZsKGvtnm4IDA0HpOFYwhE-5x69OZXQcZ57EoTO8"},
{"_id":"65eaa82b358ba96e9f0fe233","age":38,"email":"ggbond@email.com","intro":"一只猪","name":"ggbond"},
{"_id":"65eaa82b358ba96e9f0fe234","age":38,"email":"zhubajie@email.com","intro":"一直吃素的猪","name":"猪八戒"},
{"_id":"65eaa82b358ba96e9f0fe235","age":38,"email":"peiqi@email.com","intro":"一只吹风机","name":"佩奇"}]

这篇关于unicloud where 使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/788695

相关文章

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.