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.
The MongoDB $match operator filters the documents to pass only those documents that match the specified condition(s) to the next pipeline stage.
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.
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.
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.
$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
.
{
$match: {
$or:[
{'sender':sender, 'recipient':recipient},
{'recipient':sender,'sender':recipient}
]
}
}
using $or
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With