First start the mongod server;
Open a shell client:
use haha
db.haha.insert({‘a’:’aaa’})
db.haha.find()
{‘a’:’aaa’}//The addition was successful
Open another shell client:
use haha
Db.haha.find() // confirm the input just now
{‘a’:’aaa’}//It worked
As you can see, the shell client inside is effective.
The following is php code:
$m = new MongoClient();
$jihe = $m->db->haha;$cursor = $jihe->find();
foreach($cursor as $item){
var_dump($item); //nothing, empty data
bracket
Then modify the php code to read:
$m = new MongoClient();
$jihe = $m->db->haha;
$doc = array(‘b’=>’bbb’);
$jihe->insert($doc);// php tries to insert data itself
$cursor = $jihe->find();
foreach($cursor as $item){
var_dump($item); //This time only{‘b’:’bbb’}Data, no shell set inside{‘a’:’aaa’}
bracket
What is going on here? It’s all for the same database. How is there such a difference between shell client and php access?
Speculation: shell Connected
Ha-ha
, php even isdb
This is 2 database spaces.
Open a shell client and enter the commandshow dbs
Is there a db in addition to haha? If Yes
$jihe = $m->db->haha;
Should read
$jihe = $m->haha->haha;
If it is No, php hasn’t touched it for years.