What is the difference between those queries

  1. $nin vs $nor
  2. $in vs $or ,
  3. $and vs $all
    For my exercise those corresponding queries outputs the same.

The operators $nin, $in and $all are operators for arrays. The general syntax is

{ field : { $operator : [ array-of-values } }

The operators $nor, $or and $and are logical operators. The general syntax is

{ $operator : [ array-of-logical-expression ] }

Yes you can achieve the same as $nin, $in, and $all with the logical operators. But as the array of values grows it becomes unreadable. For example:

{ f : { $in : [ 1 , 3 , 5 , 7 , 11 , 13 ] } }

would be

{ $or : [ { f : 1 } , { f : 2 } , { f : 3 } , { f : 5 } … { f : 13 } ] }

however you could not do

{ $or : [ { f : { $lt : 5 } } , { f : { $gt : 10 } } ] }

with the array operators.






1 Like

Thanks @steevej-1495 for providing the detailed explanation. :clap:

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

Kind Regards,
Sonali

Thanks, @steevej-1495 for your brief explanation!! I got it.