Reputation: 388
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!
Upvotes: 5
Views: 6297
Reputation: 35
Check this one :
$pipelines = array(array('$match' => array("email" => array('$exists' => 1, '$not' => array('$size' => 0)))),array('$project' => array('_id' => 0)),array('$sample' => array('size' => 2000)));
$cursor = $collection->aggregate($pipelines);
Upvotes: 0
Reputation: 2382
You could also do this like
$records = $dbh->find(array('$where' => 'this.email && this.email.length'))->limit(2000);
Upvotes: 0
Reputation: 342
$records = $dbh->find('$or'=>array(
array('email'=>array('$exists' => false)),
array('email'=>array('$ne' => null)),
array('email'=>array('$ne' => ''))
));
Upvotes: 0
Reputation: 731
After 3 hours of failing, I managed to do it in a tricky way:
find('field' => array('$regex' => '.*'))
Upvotes: 1
Reputation: 388
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);
Upvotes: 2
Reputation: 11830
Try this, you are enclosing into array improperly
$records = $dbh->find(array("email" => array('$ne' => null)))->limit(2000);
Upvotes: 4