微信小程序云開發(fā)API 查詢指令

2022-05-12 16:11 更新

db.command.and

查詢指令,用于表示邏輯 "與" 的關系,表示需同時滿足多個查詢篩選條件

示例代碼

如篩選出進度大于 50 小于 100 的 todo:

流式寫法:

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

前置寫法:

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

db.command.or

查詢指令,用于表示邏輯 "或" 的關系,表示需同時滿足多個查詢篩選條件?;蛑噶钣袃煞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))
})

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

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

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

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

如篩選出進度大于 80 或已標為已完成的 todo:

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


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號