1.

Databases like Oracle use the “IS NULL” operator to retrieve only the records where the queried value is actually NULL (and not missing), how can the same be achieved in MongoDB?

Answer»

This can be achieved in MongoDB using the $type operator. A null value, i.e., BSON type null has the type number 10.  Using this type number, only those documents can be retrieved whose value is null. 

Take the example of the below two documents in STARTUP collection

{ _id: 1, name: "XYZ Tech", website: null },   { _id: 2, name: “ABC Pvt Ltd” }

The query { website : { $type: 10 } } will retrieve only those documents where the website is null, in the above case it WOULD be the startup “XYZ Tech”

NOTE: The query { website : null } on the other HAND will match documents where the website is null or the documents where the website field does not EXIST. For the above collection data, this query will return both the startups.



Discussion

No Comment Found