codecamp

微信小程序云开发服务端数据库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

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

方法签名:

function gt(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: _.gt(50)
    })
    .get()
  } catch(e) {
    console.error(e)
  }
}

db.command.gte

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

方法签名:

function gte(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: _.gte(50)
    })
    .get()
  } catch(e) {
    console.error(e)
  }
}

db.command.in

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

方法签名:

function in(values: any[]): Command

示例代码

找出进度为 0 或 100 的 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: _.in([0, 100])
    })
    .get()
  } catch(e) {
    console.error(e)
  }
}

db.command.in

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

方法签名:

function nin(values: any[]): Command

示例代码

找出进度不是 0 或 100 的 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: _.nin([0, 100])
    })
    .get()
  } catch(e) {
    console.error(e)
  }
}

db.command.nin

支持端:小程序 , 云函数 , Web

查询筛选操作符,表示要求值不在给定的数组内。

参数

value: any[]

返回值

Command

示例代码

找出进度不是 0 或 100 的 todo

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


微信小程序云开发服务端数据库API 地理位置构造器
微信小程序云开发服务端数据库API 查询指令
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

微信小程序 指南

目录结构

开放能力

微信小程序 调试

微信小程序 实时日志

微信小程序 小程序测速

微信小程序 基础组件

微信小程序 API

媒体

界面

微信小程序API 绘图

微信小程序 服务端

接口调用凭证

统一服务消息

微信小程序 服务市场

微信小程序 生物认证

微信小程序 云开发

服务端

微信小程序云开发服务端API 数据库

SDK文档

微信小程序 扩展能力

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }