SDK數(shù)據(jù)庫(kù) Command·查詢·邏輯操作符

2022-05-12 16:52 更新

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

支持端:小程序 , 云函數(shù) , Web

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

參數(shù)

expressions: any[]

返回值

Command

使用說(shuō)明

and 有兩種使用情況:

1. 用在根查詢條件

此時(shí)需傳入多個(gè)查詢條件,表示需同時(shí)滿足提供的多個(gè)完整查詢條件

const _ = db.command
db.collection('todo').where(_.and([
  {
    progress: _.gt(50)
  },
  {
    tags: 'cloud'
  }
])).get()

但以上用 and 組成的查詢條件是不必要的,因?yàn)閭魅氲膶?duì)象的各字段隱式組成了 “與” 的關(guān)系,上述條件等價(jià)于下方更簡(jiǎn)潔的寫法:

const _ = db.command
db.collection('todo').where({
  progress: _.gt(50),
  tags: 'cloud'
}).get()

通常需要顯示使用 and 是用在有跨字段或操作的時(shí)候,如以下表示 “progress 字段大于 50 或 tags 字段等于 cloud 或 tags 數(shù)組字段(如果 tags 是數(shù)組)中含有 cloud”:

const _ = db.command
db.collection('todo').where(_.and([
  _.or({
    progress: _.gt(50)
  }),
  _.or({
    tags: 'cloud'
  })
])).get()

2. 用在字段查詢條件

需傳入多個(gè)查詢操作符或常量,表示字段需滿足或匹配給定的條件。

如以下用前置寫法的方式表示 "progress 字段值大于 50 且小于 100"

const _ = db.command
db.collection('todo').where({
  progress: _.and(_.gt(50), _.lt(100))
})

還可以用后置寫法的方式表示同樣的條件:

const _ = db.command
db.collection('todo').where({
  progress: _.gt(50).and(_.lt(100))
})

注意 Command 默認(rèn)也可以直接鏈?zhǔn)秸{(diào)用其他 Command,默認(rèn)表示多個(gè) Command 的與操作,因此上述代碼還可以精簡(jiǎn)為:

const _ = db.command
db.collection('todo').where({
  progress: _.gt(50).lt(100)
})

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

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

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



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

支持端:小程序 , 云函數(shù) , Web

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

參數(shù)

expressions: any[]

返回值

Command

字段值的或操作

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

如篩選出進(jìn)度大于 80 或小于 20 的 todo:

流式寫法:

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

前置寫法:

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

前置寫法也可接收一個(gè)數(shù)組:

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

跨字段的或操作

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

如篩選出進(jìn)度大于 80 或已標(biāo)為已完成的 todo:

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

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

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

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

Command.not(command: Command): Command

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

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

支持端:小程序 2.8.3, 云函數(shù) 1.2.1, Web

查詢操作符,用于表示邏輯 "都不" 的關(guān)系,表示需不滿足指定的所有條件。如果記錄中沒(méi)有對(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)