I am new to Mongodb, my version is 5.0.15. I was having doubt on the projected fields in the find
query. I have the following query
db.movies.find({}, {title:1, rating:"$imdb.rating"}).sort({rating:1})
The above query doesn't sort using the rating
field. If I replace rating
with imdb.rating
then it works fine. Can someone explain why is it so?
Like even if I use fields which are not projected but in the original document, the command sorts using them as well. I know I can achieve the same using aggregate but I want to know the reason why this not works. Any link to documentation would be appreciated.
Output -
Using db.movies.find({}, {title:1, rating: "$imdb.rating"}).sort({"rating":1})
Using db.movies.find({}, {title:1, rating: "$imdb.rating"}).sort({"imdb.rating":1})