Chapter 5 sort() and limit()

db.trips.find( { “birth year”: { “$ne”:null } },{ “birth year”:1 } ).sort ( { “birth year”:-1 } ) . limit( 5 )
this query includes birth year having null values in result. Why. Help me

Hi @Anurag_Shukla, welcome to the community :wave:.
The query won’t return documents having birth year as null, but I am assuming you are talking about the documents having empty strings "" as birth year.

A quick fix would be to use the $nin operator and passing null and an empty string as the values to exclude from the results:

‘birth year’: {’$nin’:[null, “”] } }

Or may be you can try the $and operator for the same instead of $nin.

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer