Syntax Error in $match

Please help me to resolve syntax error in following query:

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

After executing below command,
db.movies.aggregate(pipeline).itcount()

I am getting this error message-
“errmsg” : “the match filter must be an expression in an object”,

Hi @Sumitha_Nair, welcome to the community.
As mentioned in the error message:

the match filter must be an expression in an object

You have to remove the square brackets from your $match stage, in order to make it an object instead of an array.

var pipeline = [
  {
    $match: {
      "imdb.rating": { $gte: 7 },
      $nor: [{ genres: "Crime" }, { genres: "Horror" }],
      $or: [{ rated: "PG" }, { rated: "G" }],
      $and: [{ languages: "English" }, { languages: "Japanese" }],
    },
  },
];

I hope it helps.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like

It works.
Thank you for your help.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.