微信小程序云开发服务端数据库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 语句,满足其中一个即可,示例:

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

阅读剩余部分

相关阅读 >>

微信小程序api wifi

微信小程序 服务端接口-商品管理接口

微信小程序api 临时素材接口

微信小程序api 录音-获取录音管理器

微信小程序 用户信息-unionid机制说明

微信小程序云开发服务端数据库api 指定查询结果集数量上限

微信小程序 广告数据源报表查询

微信小程序表单组件滑动选择器 slider

微信小程序设计规范

微信小程序api 背景音频-背景音频管理器

更多相关阅读请进入《微信小程序》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...