In what year was the youngest bike rider from the sample_training database trips collection born?

db.trips.find({“birth year”: {$ne: null}},{“birth year”: 1}).sort({ “birth year”: 1}).limit(1)

here instead of using {“birth year”: {$ne: null}} use {“birth year”: {$ne: " "}} you will get correct answer

db.trips.find({“birth year”: {$ne: " "}},{“birth year”: 1}).sort({ “birth year”: 1}).limit(1)

Hello @GURURAJ_U_K, welcome to the MongoDB community forum!

You can check for both the $ne: " " and the $ne: null conditions. For example, the query predicate can be:

{ $and: [ { year: { $ne: "" } }, { year: { $ne: null } } ] }

Your query filter can be checking based upon the data as well as the existence of the field. The check for null covers the documents with the field value as null as well as the non-existence of the field.

Is the year field a number or a string? This also matters.

You may want to include some sample documents of the collection you are querying upon, in your reply (if you have any further questions).


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