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


当前第2页 返回上一页

如筛选出进度大于 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 获取小程序页面二维码

微信小程序媒体组件 camera

微信小程序 条码二维码识别api

微信小程序 nfc

微信小程序 更新机制

微信小程序云开发api 获取数据库查询及更新指令

微信小程序api 绘图draw(进行绘图)

微信小程序api 创建插屏广告组件

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




打赏

取消

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

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

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

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

评论

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