举报投诉联系我们 手机版 热门标签 名动网
您的位置:名动网 > 微信小程序云开发获取数据库数据 微信小程序云开发服务端数据库API 查询指令

微信小程序云开发获取数据库数据 微信小程序云开发服务端数据库API 查询指令

2023-04-14 22:20

微信小程序云开发获取数据库数据 微信小程序云开发服务端数据库API 查询指令

微信小程序云开发获取数据库数据 微信小程序云开发服务端数据库API 查询指令

微信小程序云开发获取数据库数据

db.command.and

查询指令,用于表示逻辑 "与" 的关系,表示需同时满足多个查询筛选条件

示例代码

如筛选出进度大于 50 小于 100 的 todo:

流式写法:

const cloud = require("wx-server-sdk")
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
  try {
    return await db.collection("todo").where({
      progress: _.gt(50).and(_.lt(100))
    }).get()
  } catch(e) {
    console.error(e)
  }
}

前置写法:

const cloud = require("wx-server-sdk")
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
  try {
    return await db.collection("todo").where({
      memory: _.and(_.gt(50), _.lt(100))
    }).get()
  } catch(e) {
    console.error(e)
  }
}

db.command.or

查询指令,用于表示逻辑 "或" 的关系,表示需同时满足多个查询筛选条件。或指令有两种用法,一是可以进行字段值的 “或” 操作,二是也可以进行跨字段的 “或” 操作。

字段值的 “或” 操作指的是指定一个字段值为多个值之一即可:

字段值的或操作:示例代码

如筛选出进度大于 80 或小于 20 的 todo:

流式写法:

const cloud = require("wx-server-sdk")
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
  try {
    return await db.collection("todo").where({
      progress: _.gt(80).or(_.lt(20))
    }).get()
  } catch(e) {
    console.error(e)
  }
}

前置写法:

const cloud = require("wx-server-sdk")
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
  try {
    return await db.collection("todo").where({
      memory: _.or(_.gt(80), _.lt(20))
    }).get()
  } catch(e) {
    console.error(e)
  }
}

跨字段的 “或” 操作指条件 “或”,相当于可以传入多个 where 语句,满足其中一个即可,示例:

跨字段的或操作:示例代码

如筛选出进度大于 80 或已标为已完成的 todo:

const cloud = require("wx-server-sdk")
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
  try {
    return await db.collection("todo").where(_.or([
      {
        progress: _.gt(80)
      },
      {
        done: true
      }
    ]))
  } catch(e) {
    console.error(e)
  }
}

db.command.not

支持端:小程序 2.8.3, 云函数 1.2.1, Web

查询操作符,用于表示逻辑 "非" 的关系,表示需不满足指定的条件。

参数

command: Command

返回值

Command

示例

如筛选出进度不等于100的 todo:

const _ = db.command
db.collection("todo").where({
  progress: _.not(_.eq(100))
})

not 也可搭配其他逻辑指令使用,包括 and, or, nor, not,如 or:

const _ = db.command
db.collection("todo").where({
  progress: _.not(_.or([_.lt(50), _.eq(100)]))
})

db.command.nor

支持端:小程序 2.8.3, 云函数 1.2.1, Web

查询操作符,用于表示逻辑 "都不" 的关系,表示需不满足指定的所有条件。如果记录中没有对应的字段,则默认满足条件。

参数

expressions: any[]

返回值

Command

示例 1

筛选出进度既不小于20又不大于80的 todo :

const _ = db.command
db.collection("todo").where({
  progress: _.nor([_.lt(20), _.gt(80)])
})

以上同时会筛选出不存在 progress 字段的记录,如果要要求 progress 字段存在,可以用 exists 指令:

const _ = db.command
db.collection("todo").where({
  progress: _.exists().nor([_.lt(20), _.gt(80)])
  // 等价于以下非链式调用的写法:
  // progress: _.exists().and(_.nor([_.lt(20), _.gt(80)]))
}).get()

示例 2

筛选出 progress 不小于 20 且 tags 数组不包含 miniprogram 字符串的记录:

const _ = db.command
db.collection("todo").where(_.nor([{
  progress: _.lt(20),
}, {
  tags: "miniprogram",
}])).get()

以上会筛选出满足以下条件之一的记录:

  1. progress 不小于 20 且 tags 数组不包含 miniprogram 字符串
  2. progress 不小于 20 且 tags 字段不存在
  3. progress 字段不存在 且 tags 数组不包含 miniprogram 字符串
  4. progress 不小于 20 且 tags 字段不存在

如果要求 progress 和 tags 字段存在,可以用 exists 指令:

const _ = db.command
db.collection("todo").where(
  _.nor([{
    progress: _.lt(20),
  }, {
    tags: "miniprogram",
  }])
  .and({
    progress: _.exists(true),
    tags: _.exists(true),
  })
)

调用风格

方法接收两种传参方式,一是传入一个数组参数,二是传入多个参数,效果一样。

// 传入数组
function nor(expressions: Expression[]): Command
// 传入多参数
function nor(...expressions: Expression[]): Command


阅读全文
以上是名动网为你收集整理的微信小程序云开发获取数据库数据 微信小程序云开发服务端数据库API 查询指令全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
  • sdk数据库是什么 SDK数据库 database

    sdk数据库是什么 SDK数据库 database

    2023-06-23

    Cloud.database(options: Object):Database支持端:小程序,云函数,Web获取数据库实例参数options: Object属性类型默认值必填说明...

  • sdk 数据 SDK数据库 Aggregate·计算记录数

    sdk 数据 SDK数据库 Aggregate·计算记录数

    2023-06-06

    Aggregate.count(fieldName: string):Aggregate支持端:小程序 2.7.4,云函数 0.8.1,Web聚合阶段。计算上一聚合阶段输入到本阶段...

  • vue3 事件 Vue 3.0 事件处理

    vue3 事件 Vue 3.0 事件处理

    2023-04-03 VUE3教程

    #监听事件我们可以使用 v-on 指令 (通常缩写为 @ 符号) 来监听 DOM 事件,并在触发事件时执行一些 JavaScript。用法为 v-on:clic...

  • vue3 渲染函数 Vue 3.0 渲染函数

    vue3 渲染函数 Vue 3.0 渲染函数

    2023-06-08 VUE3教程

    Vue 推荐在绝大多数情况下使用模板来创建你的 HTML。然而在一些场景中,你真的需要 JavaScript 的完全编程的能力。这时你可以用...

  • vue 3.0 Vue 3.0 标准

    vue 3.0 Vue 3.0 标准

    2023-06-03 VUE3教程

    万维网联盟 (W3C) 网络可访问性倡议 (WAI) 为不同的组件制定了 Web 可访问性标准:用户代理无障碍指南 (UAAG)网络浏览器和媒体播...

© 2024 名动网 mdwl.vip 版权所有 联系我们