Grokking MongoDB
Vote

0% completed

​

Query for Null or Missing Fields

Three users are in the collection below, and a search for email: null returns two of them, even though only one has a null email.

db.users.drop() db.users.insertMany([ { name: "Alice", email: "alice@example.com" }, { name: "Bob", email: null }, { name: "Charlie" } ]) db.users.find({ email: null }, { name: 1, _id: 0 })
[ { name: 'Bob' }, { name: 'Charlie' } ]

Bob and Charlie are in different situations. Bob's document has an email field, and its value is null. Charlie's document has no email field at all, which is called a missing field

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content