在$match中是否可以使用OR?
我的意思是这样的:
db.articles.aggregate(
{ $or: [ $match : { author : "dave" }, $match : { author : "john" }] }
);
在$match中是否可以使用OR?
我的意思是这样的:
db.articles.aggregate(
{ $or: [ $match : { author : "dave" }, $match : { author : "john" }] }
);
$match: { $or: [{ author: 'dave' }, { author: 'john' }] }
因此,由于$match
运算符只是获取您通常放入find()
函数中的内容。
$or
操作符对相同字段进行操作时,$in
操作符将是更好的选择,因为它可以增加可读性。$match: {
'author': {
$in: ['dave','john']
}
}
$in
:
$or 与 $in
当使用$or与<表达式>进行相等性检查时,对于同一字段的值,请使用$in运算符而不是$or运算符。
https://docs.mongodb.com/manual/reference/operator/query/or/#or-versus-in
$match : {
$or: [
{
$expr: {
$ne: ["$community", ObjectId(id)]
}
},
{
community: {
$exists: false
}
}
]
}
{$or...}
块是一个答案,这让我感到很困惑。它并不清楚你在引用问题,但它给人的感觉好像你提供了两种解决问题的方法。在“我建议...”之前的所有内容都可以删除,这只会增加答案的清晰度。 --- 除此之外,我也认为使用 $in 运算符是正确的方法,所以我也投票支持你的答案。(否则你将使用 $or 作为阶段,而它不是)。 - Daniel F