微信小程序开发 专题
专题目录
您的位置:微信小程序开发 > 微信小程序开发专题 > 微信小程序云开发API 查询筛选条件
微信小程序云开发API 查询筛选条件
作者:--    发布时间:2019-11-20

查询筛选条件,表示字段等于某个值。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
})


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册