微信小程序云开发服务端数据库API 查询筛选条件


本文整理自网络,侵删。

db.command.eq

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

方法签名:

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'
  })
})

示例代码

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('articles').where({
      stat: _.eq({
        publishYear: 2018,
        language: 'zh-CN'
      })
    })
    .get()
  } catch(e) {
    console.error(e)
  }
}

db.command.neq

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


db.command.lt

查询筛选条件,表示字段需小于指定值。

方法签名:

function lt(value: number): Command

示例代码

找出进度小于 50 的 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('todos').where({
      progress: _.lt(50)
    })
    .get()
  } catch(e) {
    console.error(e)
  }
}

db.command.lte

查询筛选条件,表示字段需小于或等于指定值。

方法签名:

function lte(value: number): Command

示例代码

找出进度小于或等于 50 的 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('todos').where({
      progress: _.lte(50)
    })
    .get()
  } catch(e) {
    console.error(e)
  }
}

db.command.gt

查询筛选条件,表示字段需大于指定值。

阅读剩余部分

相关阅读 >>

微信小程序 广告-广告分析数据接口

微信小程序云开发sdk文档 文件存储下载文件

sdk数据库 collection请求

微信小程序api 跳转到tabbar页面

微信小程序设计规范(1) 友好礼貌

微信小程序 组件生命周期

sdk数据库 aggregate文档拆分

sdk数据库 command查询逻辑操作符

微信小程序 运维中心getscenelist

微信小程序api 性能

更多相关阅读请进入《微信小程序》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...