Find a field in MongoDB where the field is not empty (in PHP)

This can be very easy to do, but I'm pretty new to MongoDB and I have problems with the correct rule.

I pull the presenters from the database, and I just want them to retrieve entries where the email field is not empty. I tried to insert an instruction to skip a record if it is not zero, but there are many fields that are not null, but they also have no characters. Here is my MongoDB request:

$records = $dbh->find('email' => array('$ne' => null))->limit(2000);

I also tried 'email' => array('$ne' => '')) , but that didn't help either.

Is there an easy way to do this? I know that in MySQL this would be just where email != "" , But I'm still learning my way around Mongo :) Thanks!

+4
source share
5 answers

Try this, you are entering the array incorrectly

  $records = $dbh->find(array("email" => array('$ne' => null)))->limit(2000); 
+4
source

Well, I just thought about a decent way and posted it for any other "noob" like me who has the same problem :)

I used '$ nin' with an array and fixed the problem. Here is my request now:

$records = $dbh->find(array("email" => array('$nin' => array(' ','',null))))->limit(2000);

+2
source

After a 3-hour crash, I managed to do it in a complicated way:

find ('field' => array ('$ regex' => '. *'))

0
source
 $records = $dbh->find('$or'=>array( array('email'=>array('$exists' => false)), array('email'=>array('$ne' => null)), array('email'=>array('$ne' => '')) )); 
0
source

You can also do this, for example

 $records = $dbh->find(array('$where' => 'this.email && this.email.length'))->limit(2000); 
0
source

All Articles