SDK数据库 Command查询逻辑操作符


当前第2页 返回上一页

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

const _ = db.command
db.collection('todo').where(_.or([
  {
    progress: _.gt(80)
  },
  {
    done: true
  }
]))

调用风格

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

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

Command.not(command: Command): Command

支持端:小程序 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)]))
})

Command.nor(expressions: any[]): Command

支持端:小程序 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 更新指令

微信小程序 扩展组件视频滑动切换组件

微信小程序云开发服务端数据库api 在集合上新增记录

微信小程序 createqrcode

微信小程序api createworker

微信小程序滑块视图容器 滑块视图容器

微信小程序 小程序直播-其他能力

微信小程序api 绘图createlineargradient(创建线性渐变)

微信小程序 图片高清化能力

微信小程序 服务端能力

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




打赏

取消

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

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

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

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

评论

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