本文整理自网络,侵删。
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
查询筛选条件,表示字段需大于指定值。
相关阅读 >>
更多相关阅读请进入《微信小程序》频道 >>

Vue.js 设计与实现 基于Vue.js 3 深入解析Vue.js 设计细节
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者