简体   繁体   中英

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!

试试这个,你就是不正确地插入阵列

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

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);

After 3 hours of failing, I managed to do it in a tricky way:

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

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

你也可以这样做

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM