May 20, 2021 WeChat Mini Program Development Document
Support: small programs, cloud functions, web
A query operator that represents the relationship between logical "and" and that multiple query filters need to be met at the same time
And there are two uses:
Multiple query conditions need to be passed in at this point, indicating that multiple full query conditions provided need to be met at the same time
const _ = db.command
db.collection('todo').where(_.and([
{
progress: _.gt(50)
},
{
tags: 'cloud'
}
])).get()
However, the above query conditions with and are unnecessary because the fields of the incoming object implicitly form a "and" relationship, which is equivalent to the more concise writing below:
const _ = db.command
db.collection('todo').where({
progress: _.gt(50),
tags: 'cloud'
}).get()
It is often necessary to show that the use of and is used when there are spanned fields or actions, such as the following means "the progress field is greater than 50 or the tags field is equal to the cloud or tags array field (if the tags are an array) contains cloud":
const _ = db.command
db.collection('todo').where(_.and([
_.or({
progress: _.gt(50)
}),
_.or({
tags: 'cloud'
})
])).get()
Multiple query operators or constants need to be passed in, indicating that the field needs to meet or match a given condition.
If the following is a pre-write way to indicate "progress field value is greater than 50 and less than 100"
const _ = db.command
db.collection('todo').where({
progress: _.and(_.gt(50), _.lt(100))
})
You can also represent the same condition in a post-write manner:
const _ = db.command
db.collection('todo').where({
progress: _.gt(50).and(_.lt(100))
})
Note Command can also call other Commands directly chained by default, which represents multiple Commands with operations, so the above code can also be reduced to:
const _ = db.command
db.collection('todo').where({
progress: _.gt(50).lt(100)
})
Methods receive two kinds of parameters, one is to pass in an array of parameters, and the other is to pass in multiple parameters, the effect is the same.
// 传入数组
function and(expressions: Expression[]): Command
// 传入多参数
function and(...expressions: Expression[]): Command
Support: small programs, cloud functions, web
A query operator that represents a logical "or" relationship that requires multiple query filters to be met at the same time. Or instructions can be used in two ways, between "or" for field values and for cross-field "or" operations.
The "or" action of a field value refers to specifying a field value as one of more than one value.
If you filter out todos with progress greater than 80 or less than 20:
Streaming:
const _ = db.command
db.collection('todo').where({
progress: _.gt(80).or(_.lt(20))
})
Pre-writing:
const _ = db.command
db.collection('todo').where({
progress: _.or(_.gt(80), _.lt(20))
})
The pre-write method can also receive an array:
const _ = db.command
db.collection('todo').where({
progress: _.or([_.gt(80), _.lt(20)])
})
The "or" action refers to a condition or "" across a field, which is equivalent to passing in more than one where statement to satisfy one of them.
If you filter out todos with progress greater than 80 or marked as completed:
const _ = db.command
db.collection('todo').where(_.or([
{
progress: _.gt(80)
},
{
done: true
}
]))
Methods receive two kinds of parameters, one is to pass in an array of parameters, and the other is to pass in multiple parameters, the effect is the same.
// 传入数组
function or(expressions: Expression[]): Command
// 传入多参数
function or(...expressions: Expression[]): Command
Support: Small program 2.8.3, cloud function 1.2.1, Web
A query operator that represents a logical "non" relationship that does not meet the specified criteria.
If you filter out todos with progress that is not equal to 100:
const _ = db.command
db.collection('todo').where({
progress: _.not(_.eq(100))
})
Not can also be used with other logical instructions, including and, or, nor, not, such as or:
const _ = db.command
db.collection('todo').where({
progress: _.not(_.or([_.lt(50), _.eq(100)]))
})
Support: Small program 2.8.3, cloud function 1.2.1, Web
A query operator that represents a relationship with a logical "no" that does not meet all the specified criteria. If there are no fields in the record, the criteria are met by default.
Filter out todos with progress that is neither less than 20 nor greater than 80:
const _ = db.command
db.collection('todo').where({
progress: _.nor([_.lt(20), _.gt(80)])
})
The above also filters out records that do not exist in the progress field, and if you want to require the progress field to exist, you can use the exists directive:
const _ = db.command
db.collection('todo').where({
progress: _.exists().nor([_.lt(20), _.gt(80)])
// 等价于以下非链式调用的写法:
// progress: _.exists().and(_.nor([_.lt(20), _.gt(80)]))
}).get()
Filter out records with progress not less than 20 and tags arrays that do not contain miniprogram strings:
const _ = db.command
db.collection('todo').where(_.nor([{
progress: _.lt(20),
}, {
tags: 'miniprogram',
}])).get()
The above filters out records that meet one of the following criteria:
If you require the progress and tags fields to exist, you can use the exists directive:
const _ = db.command
db.collection('todo').where(
_.nor([{
progress: _.lt(20),
}, {
tags: 'miniprogram',
}])
.and({
progress: _.exists(true),
tags: _.exists(true),
})
)
Methods receive two kinds of parameters, one is to pass in an array of parameters, and the other is to pass in multiple parameters, the effect is the same.
// 传入数组
function nor(expressions: Expression[]): Command
// 传入多参数
function nor(...expressions: Expression[]): Command