57

I am trying to use the following query in MongoDB but it is not working.

  db.test.aggregate(
    $match: {
      $and: [
        type: { $in: ["TOYS"] },
        type: { $nin: ["BARBIE"] },
        time: { $lt: ISODate("2013-12-09T00:00:00Z") }
      ]
    }
  })

It says invalid character ":".

Is it possible to use $and with $match? I have seen an example on this forum of $or with $match so I presumed this is possible.

Thank you in advance for your help and guidance.

5
  • The documentation is here: docs.mongodb.org/manual/reference/operator/aggregation/match Commented Dec 9, 2013 at 11:44
  • Thanks for the reply. I gather that I do not need to use $and since it is implied in the query? Commented Dec 9, 2013 at 11:51
  • 3
    This is a query that works perfectly without aggregation framework, use a regular find Commented Dec 9, 2013 at 11:53
  • I agree with Maximiliano Rios, read this docs.mongodb.org/manual/reference/method/db.collection.find Commented Dec 9, 2013 at 11:55
  • 7
    Why suggest him to use something else, instead of helping to solve his problem? Maybe what we see is only a tiny part of his full aggregation? Commented Feb 14, 2017 at 11:31

4 Answers 4

118

$and with $match works just fine.

You have syntax errors in your query. Try this.

db.test.aggregate([
                   { 
                     $match: {
                          $and: [ 
                              {type: {$in: ["TOYS"]}}, 
                              {type: {$nin: ["BARBIE"]}}, 
                              {time: {$lt:ISODate("2013-12-09T00:00:00Z")}}
                          ]
                     }
                   }
                  ])

And for what you are trying to do, you do not need an $and.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Ajai.....thanks for the correction. And yes I found out that $and is not required. Thank you to all for helping me out.
Isn't the use $and redundant in this case? At least in MongoDB 3.2+ it seems to be.
Yes. $and is redundant here.We have it to answer the question: Is it possible to use $and with $match?
I don't think $and is entirely redundant within $match... Maybe in the most simple cases... First I tried two separate $expr conditions directly within $match, but was not getting the proper result... Had to add the $and in order to combine the comparisons properly (version 3.6.2).
In the case where you build the matching expression sequentially and have several conditions on the same parameter, using $and in the $match expression can be pretty convenient.
9
{
  $match: {
    $or:[
      {'sender':sender, 'recipient':recipient},
      {'recipient':sender,'sender':recipient}
    ]
  }
}

using $or

Comments

9
db.test.find( {$and: [ {"type": {$in: ["TOYS"]}}, 
                       {"type": {$nin: ["BARBIE"]}}, 
                       {"time": {$lt:ISODate("2013-12-09T00:00:00Z")}}
                     ]
})

AND works with FIND, receives an array of matches (but it's not a match instruction) Aggregation framework is for something completely different, it's like the word says, for aggregating (count, sum, avg, and so worth grouping or unwinding, etc)

3 Comments

Thanks for the answer Maximiliano. I used an aggregation because I needed to group by userID after matching these records. Sorry for giving an incomplete picture of the problem. So I gather $and is not possible with aggregation then....
You can use $and with aggregation but you don't have to write it, and is implicit using different filters, in fact you can pipe those filters in case one of them needs a different solution.
$match takes { <query> }. That means, it can take $and just fine.
1

example of $lookup then $match

db.orders.aggregate([
  {
    "$lookup": {
      "from": "user",
      "localField": "user",
      "foreignField": "_id",
      "as": "user"
    }
  },
  {
    "$unwind": "$user"
  },
  {
    "$match": {
      "$and": [
        {
          "privacy.mode": {
            "$gt": 0
          }
        },
        {
          "user.privacy.mode": {
            "$gt": 0
          }
        }
      ]
    }
  }
])
``

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.