Can query documents reference the document they are iterating through?

For example, if I’m doing a db.movies.find, is there a way for me to say “find all movies where the viewtime is the same as the rating plus 10” or something like that? Thanks!

Hello David,

This is just one way, using the $expr operator

const compare = ["$imdb.rating","$imdb.votes"] //tweak values
db.movies.find({"$expr":{"$eq": compare}}).count()

$expr takes this syntax { $expr: { <expression> } }

Another example from the Docs for $expr:

db.monthlyBudget.find( { $expr: { $gt: [ "$spent" , "$budget" ] } } )

If you like you can change $eq for $neq and see if the sum yields find({}), just to double check :slight_smile:

This is super helpful, thank you so much!

1 Like

You are welcome! There is a nice intro in M001 course in case you want to take a look. I believe it’s chapter 4.