# db.command.eq
Query filter, indicating that the value of a field is equal to the specified value. The eq
command supports one literal, and may be number
, boolean
, string
, object
, or array
.
Method signature:
function eq(value: any): Command
For example, to filter out all of the articles published by yourself, you can import objects:
const myOpenID = 'xxx'
db.collection('articles').where({
_openid: myOpenID
})
Or use the command:
const _ = db.command
const myOpenID = 'xxx'
db.collection('articles').where({
_openid: _.eq(openid)
})
Note: The eq
command is more flexible than object importing, and may indicate that the value of a field is equal to the specified object. For example:
// This indicates that conditions stat.publishYear == 2018 and stat.language == 'zh-CN' are matched.
db.collection('articles').where({
stat: {
publishYear: 2018,
language: 'zh-CN'
}
})
// This indicates that the stat object is equal to { publishYear: 2018, language: 'zh-CN' }.
const _ = db.command
db.collection('articles').where({
stat: _.eq({
publishYear: 2018,
language: 'zh-CN'
})
})
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)
}
}