M001 Chapter 5 Quiz 2 Sort() and Limit()

What is the difference between null and “”…
I entered the following query and received a lot of “” in the result.

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

Then I tried

MongoDB Enterprise atlas-r754o0-shard-0:PRIMARY> db.trips.find({“birth year”: {"$ne": " "}}, {“birth year”:1}).sort({“birth year”: -1})

Then I tried
db.trips.find({“birth year”: {"$ne": “”}}, {“birth year”:1}).sort({“birth year”: -1})

It wasn’t until I used the last one that the null or blank entries were filtered out.

Also… I was always taught that you don’t want nulls… Is this enforcement of not allowing nulls in mongodb a good practice?
If I was writing this to check for nulls and blanks would I chain them together like this

db.trips.find({"$or": [{“birth year”: {"$ne": “”}}, {“birth year”: {"$ne": null}}, {“birth year”:1})].sort({“birth year”: -1})

Hi @David_44057,
1.) Well, null indicates the absence of the value and empty string indicates the value is there but it is empty.

2.) There is no such enforcement as of now on not allowing nulls, but unlike SQL you can simply remove the complete key-value pair if you don’t want it there instead of putting a null in front of the key, and it would be more space efficient as compared to later one.

I hope it helps, please feel free to reach out to us in case you have any doubts.

Thanks. Regards.
Sourabh Bagrecha,
Curriculum Services Engineer.

1 Like

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