Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mongodb

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!

like image 925
ryes31 Avatar asked Apr 23 '13 02:04

ryes31


2 Answers

Try this, you are enclosing into array improperly

 $records =      $dbh->find(array("email" => array('$ne' => null)))->limit(2000);
like image 137
chandresh_cool Avatar answered Oct 05 '22 22:10

chandresh_cool


Ok, I just figured out a decent way, and will post it for any other "noob" like me who's having the same problem :)

I used '$nin' with an array and it fixed the problem. Here's my query now:

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

like image 22
ryes31 Avatar answered Oct 05 '22 20:10

ryes31