Find field in MongoDB where field is not empty (in PHP) - 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!

Try this, you are enclosing into array improperly
$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' => ''))
));

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

Related

MongoDB regex multiple query with $and

I'm having problem to achieve this.
I have this piece of code which I had tested in Rock Mongo and it works fine.
array(
'TEST' =>
array( '$in' =>
array( new MongoRegex('/^(?!TESTVALUE)/$ig'))
)
)
Above piece of code return me all documents which haven't value "TESTVALUE" for the key "TEST".
Now what I want to achieve?
First I don't know how to write piece of code to fetch all documents which haven't values "TESTVALUE" & "SECONDVALUE" for the key "TEST".
That will be something like this:
array(
'TEST' =>
array( '$in' =>
array( new MongoRegex('/^(?!TESTVALUE)/$ig'),new MongoRegex('/^(?!SECONDVALUE)/$ig') )
)
)
And also I will need above piece of code written in PHP.
Any kind of help or suggestions is welcome and will be appreciated.
Thanks in advance
I don't think you can use $in like that, but this should work:
'TEST' => array('$and' => array(
array('$regex' => new MongoRegex('/^(?!TESTVALUE)/$ig'),
array('$regex' => new MongoRegex('/^(?!TESTVALUE)/$ig')))
something to that effect (untested)
Your queries and your description seem to contradict each other. If you want a query for all documents which don't have some string as the field value, why are you using regexes? Do you not want the value to be a substring of the field value? If you just want to do a query like "find documents where the value of the TEST field is not "X" or "Y", use $nin ("not in"):
db.collection.find({ "TEST" : { "$nin" : ["X", "Y"] } })
If the field should match multiple regexes, then either combine them into a single regex or combine the conditions with $and.

Finding and Updating PHP 2D Array Elements

This is quite a novice question - I've looked at similar solutions to similar problems on here and around the internet however I can't seem to find what the problem is with this one.
I'm playing with PHP and writing a bit of code that pulls all of the mailing data out of an API. However I noticed that load times were very very slow and that was because for each mail's contents I was doing a separate query. I've since figured out how to pull what I'm looking for in one go however I'm having trouble adding the data thats returned to my 2D array that i'm storing the data in before its output.
Here is my array below;
$mailMessage = array(
array(
'mailNumber' => $mailCount,
'mailID' => $mailID,
'mailDate' => $mailDate,
'mailSender' => $mailSender,
'mailSenderName' => null,
'mailMsg' => null
));
And here is how I'm trying to update said array;
foreach ($mailMessage as &$row){
if($row['mailID'] == $getMailID){
$row['mailMsg'] = $body;
break;
}
}
So I'm checking if the mailID is the same as the one that i'm currently looking at, then if it is i'm trying to update the mailMsg part of the array on that line to be $body. I've checked the
This however is not working. No PHP error messages are flagging up - (Some times this is worse!) and i'm pretty much hitting my head off of a brick wall.
Thanks for reading & for your time,
Jamie
Is there a reason you're using a multidimensional array instead of just an associative? You're not iterating over any indexes because there aren't any present within the aggregated array format. The correct format should be :
Multidimension :
$mailMessage = array(
0 => array(
'mailNumber' => $mailCount,
'mailID' => $mailID,
'mailDate' => $mailDate,
'mailSender' => $mailSender,
'mailSenderName' => null,
'mailMsg' => null
)
);
Associative :
$mailMessage = array(
'mailNumber' => $mailCount,
'mailID' => $mailID,
'mailDate' => $mailDate,
'mailSender' => $mailSender,
'mailSenderName' => null,
'mailMsg' => null
);

PHP Mongo Query NOT NULL

Anyone know the syntax for writing a php-mongo query to use NOT NULL?
I know how to do this when I query for NULL:
<?php
$cursor = $collection->find(array("someField" => null));
Is this even possible?
Yeah, you want the $ne operator, so
$cursor = $collection->find(array("someField" => array('$ne' => null)));
Basically, the same kind of queries you would use on the Mongo console, you pass as an array to the query methods.
In your case, it could be (if you're checking that the field exists - note that the field could just be absent from the document):
array("someField" => array('$exists' => true))
Or to check if it's not equal to null:
array("someField" => array('$ne' => null))
Watch out for the $ in double quotes, since PHP will consider that a variable.

CakePHP models findFirst method issue

I'm not familiar with CakePHP too close. Recently I've ran into problem using Model. I need to get exaclty one row from database, modify some columns values and save it back. Pretty simple, right?
What do I try to do:
$condition = array('some_id_column' => $another_models_id);
$model = $this->MyModel->findFirst($condition);
BUT i get FALSE in $model variable. At hte same time
$condition = array('some_id_column' => $another_models_id);
$model = $this->MyModel->findAll($condition);
returns array. Its structure is something like:
array (
0 =>
array (
'MyModel' =>
array (
'id' => '1',
'some_id_column' => '123456',
'some_field' => 'some text',
...
),
),
I'd go with findAll if it did not return an array of arrays, but array of models (in my case - of one model). What do I want to achieve:
$condition = array('some_id_column' => $another_models_id);
$model = $this->MyModel->findFirst($condition);
$model->some_field = 'some another text';
$model->save();
Could you help me out to understand how it's usually done in CakePHP?
I'd also like to hear why findAll finds row and findFirst fails to find it... It just does not make sense to me... They should work in almost the same way and use the same database APIs...
If I can not do what I want in CakePHP, would you write a receipt how it is usually done there ?
There is no such method as findFirst.
You're probably looking for find('first', array('conditions' => array(...))).

How can i use different string value's in one function?

First i am a beginner in programming in general, i am trying to create a program for using gps locations from Lightroom on a map in googlemaps.
When i use the print the strings below ti the screen i see 5 different value's, this is also what i want, but...
I want to create also 5 different markers on the map this is done by the addMarkerByCoords Function but how can i use the 5 value per strings in the function ?
I have tried array, foreach but i cannot getting to work. The not working part can and probably will be my fault. LOL
print_r ("$Loncoord");
print_r ("$Latcoord");
print_r ("$gui");
//$map->formatOutput = true;
$map->addMarkerByCoords("$Loncoord","$Latcoord","$gui",'<b>Old Chicago</b>');
Can somebody give me a hint ?
To: Jonathan Sampson:
outputs print_r :-5.68166666667, +24.6513888889,IMG_3308,index.html,Landschap
To: Anti Veeranna I removed the " marks (and the program still works), but can you explain why this is better ?
And to the others Thank you very very much for the effort,work and really quick responses.
Assuming that this is PHP, you could us an array of arrays, and then loop.
Something like this:
$items = array(
array(
'long' => 12.34567,
'lat' => 34.56789,
'gui' => '????',
'location' => 'old chicago'
),
...
array(
'long' => 12.34567,
'lat' => 34.56789,
'gui' => '????',
'location' => 'old chicago 5'
)
);
foreach ($items as &$item) {
$map->addMarkerByCoords(
$item['long'],
$item['lat'],
$item['gui'],
$item['location']
);
}
unset($item);
$map->addMarkerByCoords(Array($Loncoord, $Latcoord, $gui, '<b>Old Chicago</b>));
??

Categories