129

Is it possible to do an OR in the $match?

I mean something like this:

db.articles.aggregate(
    { $or: [ $match : { author : "dave" }, $match : { author : "john" }] }
);
Gergely Németh
  • 1,325
  • 2
  • 9
  • 7

3 Answers3

229
$match: { $or: [{ author: 'dave' }, { author: 'john' }] }

Like so, since the $match operator just takes what you would normally put into the find() function

Preview
  • 35,317
  • 10
  • 92
  • 112
Sammaye
  • 43,242
  • 7
  • 104
  • 146
108

In this particular case, where you are $or-ing the same field, the $in operator would be a better choice, also because it increases readability:

$match: { 
  'author': { 
    $in: ['dave','john'] 
  } 
}

According to the MongoDB docs, using $in is recommended in this case:

$or versus $in

When using $or with <expressions> that are equality checks for the value of the same field, use the $in operator instead of the $or operator.

https://docs.mongodb.com/manual/reference/operator/query/or/#or-versus-in

Daniel F
  • 13,684
  • 11
  • 87
  • 116
Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164
  • 1
    According to Mongo docs, this "most effective" way of doing it for equality comparisons such as your example. See http://docs.mongodb.org/manual/reference/operator/query/or/#_S_or%22 – Mark Gibaud Jan 09 '14 at 15:31
  • 3
    The $in is the way to go – Chenna V Jul 07 '15 at 17:33
  • 1
    I believe $or is not an aggregation operator. – Paul Shapiro Apr 19 '16 at 16:35
  • @PaulShapiro: I hope you did not down voted because of this. For all please consider commenting the reason before down-voting it helps everyone. Here is the reference [Boolean Aggregation Operators -> $or](https://docs.mongodb.org/manual/reference/operator/aggregation/or/) – Amol M Kulkarni Apr 25 '16 at 06:22
  • Your answer is not correct, and as you can see, I already commented explaining where and how the answer is incorrect. – Paul Shapiro Jun 28 '16 at 23:34
  • @PaulShapiro: You did not get the context/read properly. The query which you are pointing to is from the question itself & I too know its wrong & `$or` is not aggregation operator. What I recommended is just below that. Please read it again carefully ;) You are supposed to comment on the question that its wrong :D – Amol M Kulkarni Jun 29 '16 at 06:13
  • @AmolMKulkarni TBH I also thought that your `{$or...}` block was an answer and it got me pretty confused. It's not really clear that you are quoting the question, but it gives the feeling as if you are offering two ways to solve the problem. All before "I would recommend..." could well be removed and that would only increase the clarity of the answer. --- Other than that, I also think that using the $in operator is the way to go, so I also vote for your answer. -- (else you would be using $or as a stage, which it is not). – Daniel F Sep 11 '18 at 16:23
0
$match : {
$or: [
                {
                    $expr: {
                        $ne: ["$community", ObjectId(id)]
                    }
                },
                {
                    community: {
                        $exists: false
                    }
                }
            ]

}

md Vasim
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 22 '23 at 09:00