Query and Projection

Below query is used in Lecture “Logical Operator” to demonstrate that explicit $and is necessary here , otherwise mongo will return documents which has either of condition true.

Why mongo shell won’t interpret second condition as projection ?

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

Hi @Ajay_parmar1, welcome to the community.
As per the projection documentation, $or is not a valid projection operator, you can only use the operators given below as projection operators:

$ Projects the first element in an array that matches the query condition.
$elemMatch Projects the first element in an array that matches the specified $elemMatch condition.
$meta Projects the available per-document metadata.
$slice Limits the number of elements projected from an array. Supports skip and limit slices.

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

Thanks & Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like

Thank you @SourabhBagrecha.

I was under impression that anything in between second curly braces will be treated as projection. But looks like that is not the case. Thank you for clarification.

db.collection.find( { <condtion>}, {<projection>} )

1 Like