M001 Chapter 4 Exercises Query Operators - Logic. What is correct syntax?

Find all documents where airplanes CR2 or A81 left or landed in the KZN
airport.

My solution:

db.routes.find({"$and":[{"$or":[{"airplane":{"CR2","A81"} }] , "$or":[{ {"src_airport":"KZN"},{"dst_airport:"KZN"} }] }] }).pretty()

Error:
SyntaxError: missing : after property id @(shell):1:50

Hi @Varun_Khanna, welcome to the community.

The above syntax is invalid, you can NOT pass multiple values to an object, use an array instead, with $in operator.

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Hi @SourabhBagrecha, appreciate the response.

I went through the entire chapter 4, but could not find mention of any $in operator. It would be helpful if you could guide me to a resource concerning this operator.
Meanwhile, I tried this:

db.routes.find({"$and":[{"$or":[{"airplane":"CR2"},{"airplane":"A81"}],"$and":[{"dst_airport":"KZN"}, {"src_airport":"KZN"}] }] }).pretty()

It did not return any result and also did not throw any error.
Although possible that I do not get any results back. I feel that the purpose of the exercise would not be met. Besides, there are close to 67 thousand documents in that collection.

Anything that I might be doing wrong?

The link was already provided in

If you click on the green coloured $in you will end up on the documentation of the $in operator.

In your query you $and the *_airport rather than $or them. So rather than

you are supposed to

{ "$or" :[ { "dst_airport": "KZN" },
           { "src_airport": "KZN" } ] }

as show in the text box at the bottom of the page after the video of the lesson.

1 Like

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