微信小程序云开发API 查询筛选条件

2022-05-12 16:10 更新

db.command.eq

查询筛选条件,表示字段等于某个值。eq 指令接受一个字面量 (literal),可以是 number, boolean, string, object, array, Date。

方法签名:

function eq(value: any): Command

比如筛选出所有自己发表的文章,除了用传对象的方式:

const myOpenID = 'xxx'
db.collection('articles').where({
  _openid: myOpenID
})

还可以用指令:

const _ = db.command
const myOpenID = 'xxx'
db.collection('articles').where({
  _openid: _.eq(openid)
})

注意 eq 指令比对象的方式有更大的灵活性,可以用于表示字段等于某个对象的情况,比如:

// 这种写法表示匹配 stat.publishYear == 2018 且 stat.language == 'zh-CN'
db.collection('articles').where({
  stat: {
    publishYear: 2018,
    language: 'zh-CN'
  }
})
// 这种写法表示 stat 对象等于 { publishYear: 2018, language: 'zh-CN' }
const _ = db.command
db.collection('articles').where({
  stat: _.eq({
    publishYear: 2018,
    language: 'zh-CN'
  })
})

db.command.neq

表示字段不等于某个值,和 db.command.eq 相反


db.command.lt

查询筛选条件,表示字段需小于指定值。可以传入 Date 对象用于进行日期比较。

方法签名:

function lt(value: number | Date): Command

示例代码

找出进度小于 50 的 todo

const _ = db.command
db.collection('todos').where({
  progress: _.lt(50)
})
.get({
  success: console.log,
  fail: console.error
})

db.command.lte

查询筛选条件,表示字段需小于或等于指定值。可以传入 Date 对象用于进行日期比较。

方法签名:

function lte(value: number | Date): Command

示例代码

找出进度小于或等于 50 的 todo

const _ = db.command
db.collection('todos').where({
  progress: _.lte(50)
})
.get({
  success: console.log,
  fail: console.error
})

db.command.gt

查询筛选条件,表示字段需大于指定值。可以传入 Date 对象用于进行日期比较。

方法签名:

function gt(value: number | Date): Command

示例代码

找出进度大于 50 的 todo

const _ = db.command
db.collection('todos').where({
  progress: _.gt(50)
})
.get({
  success: console.log,
  fail: console.error
})

db.command.gte

查询筛选条件,表示字段需大于或等于指定值。可以传入 Date 对象用于进行日期比较。

方法签名:

function gte(value: number | Date): Command

示例代码

找出进度大于或等于 50 的 todo

const _ = db.command
db.collection('todos').where({
  progress: _.gte(50)
})
.get({
  success: console.log,
  fail: console.error
})

db.command.in

查询筛选条件,表示字段的值需在给定的数组内。

方法签名:

function in(values: any[]): Command

示例代码

找出进度为 0 或 100 的 todo

const _ = db.command
db.collection('todos').where({
  progress: _.in([0, 100])
})
.get({
  success: console.log,
  fail: console.error
})

db.command.in

查询筛选条件,表示字段的值需不在给定的数组内。

方法签名:

function nin(values: any[]): Command

示例代码

找出进度不是 0 或 100 的 todo

const _ = db.command
db.collection('todos').where({
  progress: _.nin([0, 100])
})
.get({
  success: console.log,
  fail: console.error
})


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号