Search code examples
phpmongodb

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


This may be a very easy thing to do, but I'm pretty new to MongoDB and am having trouble making this word right.

I'm pulling leads from a database, and I only want it to pull records where the email field is not empty. I've tried putting in the statement to skip the record if it's not null, but there are many fields that aren't null, but don't have any characters in them, either. Here is my MongoDB query:

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

I also tried 'email' => array('$ne' => '')) but that didn't do the trick either.

Is there a simple way to do this? I know in MySQL it would be a simple where email != "" but I'm still learning my way around Mongo :) Thanks!


Solution

  • Try this, you are enclosing into array improperly

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