unicloud9-command查询筛选指令比较和逻辑运算

190 阅读3分钟

where查询

文档在:unicloud--云函数通过传统方式操作数据库--获取集合的引用

image.png

之前学的doc只能通过id查询,where能查询更多条件,可以完全替代doc

通过id查询:

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

image.png

通过age查询

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

image.png

通过多个条件进行关联查询

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

image.png

where配合command查询

查询筛选指令 Query Command 可以进行比较运算和逻辑运算

以下指令挂载在 db.command 下

类型接口说明
比较运算eq字段等于 ==
neq字段不等于 !=
gt字段大于 >
gte字段大于等于 >=
lt字段小于 <
lte字段小于等于 <=
in字段值在数组里
nin字段值不在数组里
逻辑运算and表示需同时满足指定的所有条件
or表示需同时满足指定条件中的至少一个

这个方法查询数据是把整条数据的所有字段全部获取到,然后再查询

比较运算 ==

'use strict';
// 连接数据库
const db = uniCloud.database();
// 要使用command这个方法,要先创建一个变量接收对象的实例化
const dbCmd = db.command;
exports.main = async (event, context) => {
  // await 等待异步请求拿到结果之后再返回
	let res = await db.collection("users").where({
    // eq: == 查询年龄等于30的 
    age:dbCmd.eq(30)
  }).get();
  return res;
}; 

image.png

neq不等于

'use strict';
// 连接数据库
const db = uniCloud.database();
// 要使用command这个方法,要先创建一个变量接收对象的实例化
const dbCmd = db.command;
exports.main = async (event, context) => {
  // await 等待异步请求拿到结果之后再返回
	let res = await db.collection("users").where({
    // neq: != 查询年龄不等于30的 
    age:dbCmd.neq(30)
  }).get();
  return res;
}; 

image.png

gt 大于

'use strict';
// 连接数据库
const db = uniCloud.database();
// 要使用command这个方法,要先创建一个变量接收对象的实例化
const dbCmd = db.command;
exports.main = async (event, context) => {
  // await 等待异步请求拿到结果之后再返回
	let res = await db.collection("users").where({
    // gt: > 查询年龄大于30的 
    age:dbCmd.gt(30)
  }).get();
  return res;
}; 

image.png

gte 大于等于

'use strict';
// 连接数据库
const db = uniCloud.database();
// 要使用command这个方法,要先创建一个变量接收对象的实例化
const dbCmd = db.command;
exports.main = async (event, context) => {
  // await 等待异步请求拿到结果之后再返回
	let res = await db.collection("users").where({
    // gte: >= 查询年龄大于等于30的 
    age:dbCmd.gte(30)
  }).get();
  return res;
}; 

image.png

in 字段的值是否在in这个数组中,nin查询值不在数组中的字段

这个查询的值是固定写死的,要查在某一个区间是用andor

'use strict';
// 连接数据库
const db = uniCloud.database();
// 要使用command这个方法,要先创建一个变量接收对象的实例化
const dbCmd = db.command;
exports.main = async (event, context) => {
  // await 等待异步请求拿到结果之后再返回
	let res = await db.collection("users").where({
    // in: 查询年龄是30岁或45岁的 
    age:dbCmd.in([30, 45])
  }).get();
  return res;
}; 

年龄为30和45的三个人被查询出来了 image.png

and查询在某一个区间的值 uniapp提供了流式写法和前置写法:

其实不同点就是and方法包裹一个条件还是两个条件的区别

流式写法:

memory: dbCmd.gt(4).and(dbCmd.lt(32))

前置写法:

memory: dbCmd.and(dbCmd.gt(4), dbCmd.lt(32))

查询年龄大于30小于45的

'use strict';
// 连接数据库
const db = uniCloud.database();
// 要使用command这个方法,要先创建一个变量接收对象的实例化
const dbCmd = db.command;
exports.main = async (event, context) => {
  // await 等待异步请求拿到结果之后再返回
	let res = await db.collection("users").where({
    // and: 查询年龄是大于30岁、小于45岁区间的
    // age:dbCmd.gt(30).and(dbCmd.lt(45))
    age:dbCmd.and(dbCmd.gt(30),dbCmd.lt(45))
  }).get();
  return res;
}; 

返回值: image.png

and和or可以嵌套使用 官方示例:筛选出小于4000或者在6000-8000这个范围的字段

price:dbCmd.lt(4000).or(dbCmd.gt(6000).and(dbCmd.lt(8000)))