I got a very basic Mongodb Find request which seems not to work. php 7+ I want something like in SQL: where 'common' LIKE %Burk%
The simple query is to call the countries collection
and produce Burkina Faso as output
QUERY 1:
$countries_tb= $db->selectCollection('country_city_data');
$countries = $countries_tb->find([],[
'common' => new MongoDB\BSON\Regex('Burk')]);
var_dump($countries);
the var_dump
prints everything in the database
QUERY 2:
when I do find(without the empty []), it var_dumps nothing necessary
$countries = $countries_tb->find(/*without the []*/ [
'common' => new MongoDB\BSON\Regex('Burk')]);
var_dump($countries);
I get this:
I have also tried this format.
QUERY 3:
$countries = $countries_tb->find(
array('name'=>
array( 'common'=>new MongoDB\BSON\Regex('Burk'))));
var_dump($countries);
Still no success.
As suggested by
@noobProgrammer
below, doing like this:
$countries_tb->find(['name' =>
['common'=>new MongoDB\BSON\Regex('Burk')]],
['typeMap' => ['root' => 'array', 'document' => 'array']]);
Produces this;
instead of the word 'Burkina Faso'
Still NO Success
I got it working finally!
This is what I did.
$countries = $countries_tb->find(array('name.common' => array('$regex' => 'Burk') ) );
Notice the .
in name.common
Here is my foreach
loop'
foreach( $countries as $country => $name) {
foreach ($name as $n){
if(isset($n->common)){
var_dump($n->common);
}
}
}
//It prints out: country_cities.php:90:string 'Burkina Faso' (length=12)
Somehow, this does NOT work
$countries = $countries_tb->find(
array('name'=>
array('common' =>
array('$regex' => 'Burk')) ) );
{"_id":"55a0f42f20a4d760b5fc306d","altSpellings":["BF"],"area":272967,"borders":["BEN","CIV","GHA","MLI","NER","TGO"],"callingCode":["226"],"capital":"Ouagadougou","cca2":"BF","cca3":"BFA","ccn3":"854","cioc":"BUR","currency":["XOF"],"demonym":"Burkinabe","landlocked":true,"languages":{"fra":"French"},"latlng":[13,-2],"name":{"common":"Burkina Faso","native":{"fra":{"common":"Burkina Faso","official":"Burkina Faso"}},"official":"Burkina Faso"},"region":"Africa","subregion":"Western Africa","tld":[".bf"],"translations":{"cym":{"common":"Burkina Faso","official":"Burkina Faso"},"deu":{"common":"Burkina Faso","official":"Burkina Faso"},"fin":{"common":"Burkina Faso","official":"Burkina Faso"},"fra":{"common":"Burkina Faso","official":"Burkina Faso"},"hrv":{"common":"Burkina Faso","official":"Burkina Faso"},"ita":{"common":"Burkina Faso","official":"Burkina Faso"},"jpn":{"common":"ブルキナファソ","official":"ブルキナファソ"},"nld":{"common":"Burkina Faso","official":"Burkina Faso"},"por":{"common":"Burkina Faso","official":"Burkina Faso"},"rus":{"common":"Буркина-Фасо","official":"Буркина -Фасо"},"spa":{"common":"Burkina Faso","official":"Burkina Faso"}}}
All filters should be in the first array parameter
$countries_tb->find(['area' => 272967, 'capital' => 'Ouagadougou'] , options );
The second param is for options. You should pass typeMap as an option to convert the object to an array
$options = ['typeMap' => ['root' => 'array', 'document' => 'array']]
;
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.