May 20, 2021 WeChat Mini Program Development Document
A query filter that means that a field is equal to a value. The eq instruction accepts a literal amount, which can be number, boolean, string, object, array.
Method signature:
function eq(value: any): Command
For example, filter out all your own published articles, except in the form of a pass-through:
const myOpenID = 'xxx'
db.collection('articles').where({
_openid: myOpenID
})
You can also use instructions:
const _ = db.command
const myOpenID = 'xxx'
db.collection('articles').where({
_openid: _.eq(openid)
})
Note that eq instructions are more flexible than the way objects are used to indicate that a field is equal to an object, such as:
// 这种写法表示匹配 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'
})
})
The sample code
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)
}
}
Indicates that the field is not equal to a value, as opposed to db.command.eq
Query filters that indicate that the field needs to be less than the specified value.
Method signature:
function lt(value: number): Command
The sample code
Find todos with a progress of less than 50
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)
}
}
A query filter that indicates that a field needs to be less than or equal to the specified value.
Method signature:
function lte(value: number): Command
The sample code
Find todos with progress of less than or equal to 50
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)
}
}
Query filter, which means that the field needs to be greater than the specified value.
Method signature:
function gt(value: number): Command
The sample code
Find todos with progress greater than 50
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)
}
}
A query filter that indicates that a field needs to be greater than or equal to the specified value.
Method signature:
function gte(value: number): Command
The sample code
Find todos with progress greater than or equal to 50
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)
}
}
A query filter that indicates that the value of a field needs to be within a given array.
Method signature:
function in(values: any[]): Command
The sample code
Find todos with a progress of 0 or 100
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)
}
}
A query filter that indicates that the value of a field needs to be not in a given array.
Method signature:
function nin(values: any[]): Command
The sample code
Find out if the progress is not 0 or 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)
}
}
Support: small programs, cloud functions, web
The query filter operator, which means that the required value is not in a given array.
Find out if the progress is not 0 or 100 todo
const _ = db.command
db.collection('todos').where({
progress: _.nin([0, 100])
})
.get({
success: console.log,
fail: console.error
})