Lab - $match()(Getting an error while running my query))

Problem:

Help MongoDB pick a movie our next movie night! Based on employee polling, we’ve decided that potential movies must meet the following criteria.

  • imdb.rating is at least 7
  • genres does not contain “Crime” or “Horror”
  • rated is either “PG” or “G”
  • languages contains “English” and “Japanese”

My Query:
var pipeline=[{$match:{“imbd.rating”:{$gte:7},“genres”:{$ne:[“Crime”,“Horror”]},“rated”:{$or:[“PG”,“G”]},“languages”:{$and:[“English”,“Japanese”]}}}]

Am getting an error when i try this query.

1 Like

What is it?

E QUERY [js] uncaught exception: Error: command failed: {
“operationTime” : Timestamp(1585550841, 1),
“ok” : 0,
“errmsg” : “unknown operator: $or”,
“code” : 2,
“codeName” : “BadValue”,
“$clusterTime” : {
“clusterTime” : Timestamp(1585550841, 1),
“signature” : {
“hash” : BinData(0,“DmGdWNmw3tLsCyhrdu8SNzn+vm0=”),
“keyId” : NumberLong(“6763648209215553537”)
}
}
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:583:17
assert.commandWorked@src/mongo/shell/assert.js:673:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1012:12
@(shell):1:1

The problem is

Why is it a unknown operator?
did i use it in a wrong place?
Is the query correct?

Because it is not recognized in the given context.

Definitively, otherwise it will have been recognized.

Definitively, otherwise you would not get an error.

It is best to look at the documentation.

https://docs.mongodb.com/manual/reference/operator/query-array/
https://docs.mongodb.com/manual/reference/operator/query-logical/

var pipeline=[{$match:{$and:[{“imbd.rating”:{$gte:7}},{“genres”:{$nin:[“Crime”,“Horror”]}},{“rated”:{$in:[“PG”,“G”]}},{“languages”:{$all:[“English”,“Japanese”]}}]}}]

this is the query i modified adn when i hit “db.movies.aggregate(pipeline).itcount();”
Am getting the count as 0
why?

Try divide and conquer to find your problem.

Start by $match that filters on imdb.rating first, see what you get. Then check it for genres to see if the problem is that part. You do each part like that and then you start mixing them. Defining different parts as a variable helps playing around.

Also make sure you are connected to the appropriate cluster and that you use the appropriate database.

2 Likes

Hi @Aditya_Sriivatsalan_68872,

I would also recommend you to break your query and ensure that you connected to the cluster using the correct connection string.

Hint: Remove $and from the query as it makes query more complicated. Multiple comma separated query conditions are by default considered to be logically ANDed

Please feel free to reach out if you have any questions.

Thanks,
Sonali

Hello,
I’m difficulty with the same query and I haven’t figured out what might be wrong yet.

I tried running the queries pipeline in bit to see the results it returns but the query keeps running on my terminal without returning any results.

Her is my query with only one stage in the pipeline:
var pipeline = [
{
$match: {
“imbd.rating”: { $gte: 7}
}
}
]

The query with the four stages of the pipeline:

Please, any clue on what I might be doing wrong?

You might be connected to the wrong cluster, or using the wrong database or specifying the wrong collection.

What step are you doing after var pipeline = [ … ] ;?

Thank you @steevej-1495 for replying.

I discovered my connection was actually being blocked by a firewall.
I’ve been able to connect after disabling it.

Thanks

1 Like