查询指令,用于表示逻辑 "与" 的关系,表示需同时满足多个查询筛选条件
示例代码
如筛选出进度大于 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)
  }
}查询指令,用于表示逻辑 "或" 的关系,表示需同时满足多个查询筛选条件。或指令有两种用法,一是可以进行字段值的 “或” 操作,二是也可以进行跨字段的 “或” 操作。
字段值的 “或” 操作指的是指定一个字段值为多个值之一即可:
字段值的或操作:示例代码
如筛选出进度大于 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)
  }
}支持端:小程序 2.8.3, 云函数 1.2.1, Web
查询操作符,用于表示逻辑 "非" 的关系,表示需不满足指定的条件。
如筛选出进度不等于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)]))
})支持端:小程序 2.8.3, 云函数 1.2.1, Web
查询操作符,用于表示逻辑 "都不" 的关系,表示需不满足指定的所有条件。如果记录中没有对应的字段,则默认满足条件。
筛选出进度既不小于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()
筛选出 progress 不小于 20 且 tags 数组不包含 miniprogram 字符串的记录:
const _ = db.command
db.collection('todo').where(_.nor([{
  progress: _.lt(20),
}, {
  tags: 'miniprogram',
}])).get()
以上会筛选出满足以下条件之一的记录:
如果要求 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(c) 2024 chaojicainiao.com MIT license