chapter1HW of m121

I need help here; Already spent a lot of time on this!!
image
this aggregation is returning 24 results; I have gon through the result manually by scrolling !! still didn’t find whats wrong with it? Please provide your inputs. Thanks
image → question

→ count; I need 23 as per Hint

None of the $exists:true are required. But they do not change the result. They could be written as

rated : { $exists : true , $in : [ "PG" , "G" ]}

We do not see the values you have in the array for genres but sine the other field’s queries seem okay, it must be the culprit. However, I am pretty sure you have the correct values. The issue is with the logic of the operation.

thanks for your response. :+1:
the array which is not visible in last image is
image
which seems correct,

  • appreciate you for letting me know regarding $exists: true requirement and syntax.
  • I didn’t get the last line, logic of operation… what does that mean here…?

The following logic

genres : { "$ne" : { "$or" : [ "Horror" , "Crime" ] } }

The operator “$or” requires an array of expressions and returns a true or false result. Horror and Crime are not expressions. But most languages will consider them as expression evaluated to true. As shown in mongosh:

if( "Horror" ) console.log( "true" )
true

So the result of { "$or" : [ "Horror" , "Crime" ] } will probably be true for all documents.

So you end up doing genres : { $ne : true }, but genres is an array so most likely all documents have genres:{$ne:true}.

What you want is an array operator in the same family as $all and $in.

everything in a $match are already combined with and operation. $match:{a:1,b:{$in:["one","two"]}} wil match (a=1 and (b="one" or b="two"))

you may also want to use $nin

1 Like

Yes but some cases needs special care.

The following query is wrong using explicit AND.

{ "value" : { "$gt" : 10 } , "value" : { "$lt" : 100 } }

You end up with { “value” : { “$lt” : 100 } } as most JSON implementation only keep the last occurence of a key. In this case you would need explicit AND.

{ "$and" : [ { "value" : { "$gt" : 10 } } , { "value" : { "$lt" : 100 } } ] } 

But good for us, implicit AND is also available at the field level so you could write the same query as

{ "value" : { "$gt" : 10 , "$lt" : 100 } } 

For the same reason, you could not use implicit AND when AND-ing 2 or more OR conditions. The following will not work:

{ "$or" : [ c1 , c2 ]  , "$or" : [ c3 , c4 ] }

The parsed query ends up being:

{ "$or" : [ c3 , c4 ] }

You have no choice but to use explicit AND in this case.

You can always use explain plan query planner to see what query you end up with:

mongosh> db.test.find( { "value" : { "$gt" : 10 } ,
  "value" : { "$lt" : 100 } } ).explain().queryPlanner.parsedQuery
< { value: { '$lt': 100 } }

2 Likes

Thanks a lot for such a detailed explanation, finally got the required result;
tried $nin logic for genres, and yes $exists is not required,
If field is not present in document and we have a filtration criteria based on value of that field , then that document will be filtered out automatically…

1 Like

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