微信小程序云開發(fā)服務(wù)端數(shù)據(jù)庫API 查詢指令

2022-05-12 16:21 更新

db.command.and

查詢指令,用于表示邏輯 "與" 的關(guān)系,表示需同時(shí)滿足多個(gè)查詢篩選條件

示例代碼

如篩選出進(jìn)度大于 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

查詢指令,用于表示邏輯 "或" 的關(guān)系,表示需同時(shí)滿足多個(gè)查詢篩選條件?;蛑噶钣袃煞N用法,一是可以進(jìn)行字段值的 “或” 操作,二是也可以進(jìn)行跨字段的 “或” 操作。

字段值的 “或” 操作指的是指定一個(gè)字段值為多個(gè)值之一即可:

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

如篩選出進(jìn)度大于 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)
  }
}

跨字段的 “或” 操作指條件 “或”,相當(dāng)于可以傳入多個(gè) where 語句,滿足其中一個(gè)即可,示例:

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

如篩選出進(jìn)度大于 80 或已標(biāo)為已完成的 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, 云函數(shù) 1.2.1, Web

查詢操作符,用于表示邏輯 "非" 的關(guān)系,表示需不滿足指定的條件。

參數(shù)

command: Command

返回值

Command

示例

如篩選出進(jìn)度不等于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, 云函數(shù) 1.2.1, Web

查詢操作符,用于表示邏輯 "都不" 的關(guān)系,表示需不滿足指定的所有條件。如果記錄中沒有對(duì)應(yīng)的字段,則默認(rèn)滿足條件。

參數(shù)

expressions: any[]

返回值

Command

示例 1

篩選出進(jìn)度既不小于20又不大于80的 todo :

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

以上同時(shí)會(huì)篩選出不存在 progress 字段的記錄,如果要要求 progress 字段存在,可以用 exists 指令:

const _ = db.command
db.collection('todo').where({
  progress: _.exists().nor([_.lt(20), _.gt(80)])
  // 等價(jià)于以下非鏈?zhǔn)秸{(diào)用的寫法:
  // progress: _.exists().and(_.nor([_.lt(20), _.gt(80)]))
}).get()

示例 2

篩選出 progress 不小于 20 且 tags 數(shù)組不包含 miniprogram 字符串的記錄:

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

以上會(huì)篩選出滿足以下條件之一的記錄:

  1. progress 不小于 20 且 tags 數(shù)組不包含 miniprogram 字符串
  2. progress 不小于 20 且 tags 字段不存在
  3. progress 字段不存在 且 tags 數(shù)組不包含 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),
  })
)

調(diào)用風(fēng)格

方法接收兩種傳參方式,一是傳入一個(gè)數(shù)組參數(shù),二是傳入多個(gè)參數(shù),效果一樣。

// 傳入數(shù)組
function nor(expressions: Expression[]): Command
// 傳入多參數(shù)
function nor(...expressions: Expression[]): Command


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)