I am trying to find all the documents where email exists.
I am trying the following find query:
{ "email": {$exists:true, $ne:null, $ne:""}}
Still I am getting documents where email is null
.
Can anyone tell me what I am doing wrong?
The IS NOT NULL condition is used in SQL to test for a non-NULL value. It returns TRUE if a non-NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.
SELECT column_names FROM table_name WHERE column_name IS NOT NULL; Query: SELECT * FROM Student WHERE Name IS NOT NULL AND Department IS NOT NULL AND Roll_No IS NOT NULL; To exclude the null values from all the columns we used AND operator.
sql - Not equal <> !=
SELECT * FROM yourTableName WHERE yourSpecificColumnName IS NULL OR yourSpecificColumnName = ' '; The IS NULL constraint can be used whenever the column is empty and the symbol ( ' ') is used when there is empty value.
You want $nin
here:
.find({ "email": { "$nin": [ null, "" ] } })
The problem is $ne
is repeated twice and overwrites. You could use $or
but $nin
is shorter:
Given:
{
"_id" : ObjectId("59633c28f5f11516540d118e"),
"a" : 1.0
}
{
"_id" : ObjectId("59633c28f5f11516540d118f"),
"a" : 1.0,
"email" : ""
}
{
"_id" : ObjectId("59633c28f5f11516540d1190"),
"a" : 1.0,
"email" : null
}
{
"_id" : ObjectId("59633c28f5f11516540d1191"),
"a" : 1.0,
"email" : "fred"
}
Just returns:
{
"_id" : ObjectId("59633c28f5f11516540d1191"),
"a" : 1.0,
"email" : "fred"
}
Also you do not also need $exists
when you are actually testing a value. It's already implied that is does "exist".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With