Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using $and with $match in mongodb

Tags:

mongodb

match

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.

like image 410
Manus Avatar asked Dec 09 '13 11:12

Manus


People also ask

What does $match do in MongoDB?

The MongoDB $match operator filters the documents to pass only those documents that match the specified condition(s) to the next pipeline stage.

Can we use $match in Find MongoDB?

Basically, MongoDB provides the different match operators such as $match and $count, etc, to the user and we can utilize them as per our requirement. We can also use a match operator for the aggregation pipeline. In the above syntax, we use the $match Mongodb operator as shown.

What does $and do in MongoDB?

MongoDB provides different types of logical query operators and $and operator is one of them. This operator is used to perform logical AND operation on the array of one or more expressions and select or retrieve only those documents that match all the given expression in the array.

How do I match a query in MongoDB?

MongoDB $match aggregation There are two key components of this syntax: First, we call the $match operator. We then specify our query conditions. The 'the_query' represents the same type of query that can be used against MongoDB documents.


3 Answers

$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.

like image 112
4J41 Avatar answered Oct 23 '22 10:10

4J41


{
  $match: {
    $or:[
      {'sender':sender, 'recipient':recipient},
      {'recipient':sender,'sender':recipient}
    ]
  }
}

using $or

like image 27
alfonsoolavarria Avatar answered Oct 23 '22 10:10

alfonsoolavarria


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)

like image 7
Maximiliano Rios Avatar answered Oct 23 '22 09:10

Maximiliano Rios