MongoDB: How to query for records where field is null or not set?

If the sent_at field is not there when its not set then: db.emails.count({sent_at: {$exists: false}}) If it’s there and null, or not there at all: db.emails.count({sent_at: null}) If it’s there and null: db.emails.count({sent_at: { $type: 10 }}) The Query for Null or Missing Fields section of the MongoDB manual describes how to query for null … Read more

What is easier to read in EXISTS subqueries? [closed]

Intuitive is …EXISTS (SELECT * .. because you really don’t care The only keyword of importance is EXISTS The choice of …EXISTS (SELECT 1 .. perpetuates the general myths and superstitions around EXISTS (eg comments on the MySQL docs). ANSI standard says “doesn’t matter” It’s more interesting to understand that EXISTS is a semi-join.

SQL – IF EXISTS UPDATE ELSE INSERT INTO

Create a UNIQUE constraint on your subs_email column, if one does not already exist: ALTER TABLE subs ADD UNIQUE (subs_email) Use INSERT … ON DUPLICATE KEY UPDATE: INSERT INTO subs (subs_name, subs_email, subs_birthday) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE subs_name = VALUES(subs_name), subs_birthday = VALUES(subs_birthday) You can use the VALUES(col_name) function in the … Read more