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

相关文章

Spring Boot整合Redis注解实现增删改查功能(Redis注解使用)

《SpringBoot整合Redis注解实现增删改查功能(Redis注解使用)》文章介绍了如何使用SpringBoot整合Redis注解实现增删改查功能,包括配置、实体类、Repository、Se... 目录配置Redis连接定义实体类创建Repository接口增删改查操作示例插入数据查询数据删除数据更

使用python生成固定格式序号的方法详解

《使用python生成固定格式序号的方法详解》这篇文章主要为大家详细介绍了如何使用python生成固定格式序号,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... 目录生成结果验证完整生成代码扩展说明1. 保存到文本文件2. 转换为jsON格式3. 处理特殊序号格式(如带圈数字)4

Java使用Swing生成一个最大公约数计算器

《Java使用Swing生成一个最大公约数计算器》这篇文章主要为大家详细介绍了Java使用Swing生成一个最大公约数计算器的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下... 目录第一步:利用欧几里得算法计算最大公约数欧几里得算法的证明情形 1:b=0情形 2:b>0完成相关代码第二步:加

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

Linux join命令的使用及说明

《Linuxjoin命令的使用及说明》`join`命令用于在Linux中按字段将两个文件进行连接,类似于SQL的JOIN,它需要两个文件按用于匹配的字段排序,并且第一个文件的换行符必须是LF,`jo... 目录一. 基本语法二. 数据准备三. 指定文件的连接key四.-a输出指定文件的所有行五.-o指定输出

Linux jq命令的使用解读

《Linuxjq命令的使用解读》jq是一个强大的命令行工具,用于处理JSON数据,它可以用来查看、过滤、修改、格式化JSON数据,通过使用各种选项和过滤器,可以实现复杂的JSON处理任务... 目录一. 简介二. 选项2.1.2.2-c2.3-r2.4-R三. 字段提取3.1 普通字段3.2 数组字段四.

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV