【发布时间】:2013-05-29 23:15:51
【问题描述】:
可以在 $match 中做 OR 吗?
我的意思是这样的:
db.articles.aggregate(
{ $or: [ $match : { author : "dave" }, $match : { author : "john" }] }
);
【问题讨论】:
标签: mongodb aggregation-framework
可以在 $match 中做 OR 吗?
我的意思是这样的:
db.articles.aggregate(
{ $or: [ $match : { author : "dave" }, $match : { author : "john" }] }
);
【问题讨论】:
标签: mongodb aggregation-framework
$match: { $or: [{ author: 'dave' }, { author: 'john' }] }
就像这样,因为 $match 运算符只接受您通常放入 find() 函数的内容
【讨论】:
在这种特殊情况下,如果您是$or-ing 相同的字段,$in 运算符将是更好的选择,还因为它提高了可读性:
$match: {
'author': {
$in: ['dave','john']
}
}
根据 MongoDB 文档,在这种情况下建议使用 $in:
$or 与 $in
当使用 $or 和
是相等的 检查同一字段的值,请改用 $in 运算符 $or 运算符。
https://docs.mongodb.com/manual/reference/operator/query/or/#or-versus-in
【讨论】: