Start the Mongod server first;
Open a Shell client:
Use haha
Db.haha.insert ({' A ': ' AAA '})
Db.haha.find ()
{' A ': ' AAA '} //Added successfully
Open another shell client:
Use haha
Db.haha.find ()//confirm the input you just made
{' A ': ' AAA '} //is valid
As you can see, the shell client is effective.
Here is the PHP code:
$m = new Mongoclient ();
$jihe = $m->db->haha;
$cursor = $jihe->find ();
foreach ($cursor as $item) {
Var_dump ($item); Nothing, empty data
}
Then modify the PHP code to:
$m = new Mongoclient ();
$jihe = $m->db->haha;
$doc = Array (' b ' = ' BBB ');
$jihe->insert ($doc); PHP tries to insert the data itself
$cursor = $jihe->find ();
foreach ($cursor as $item) {
Var_dump ($item); This time only {' B ': ' BBB '} data, no shell set {' A ': ' AAA '}
}
What's going on here? is for the same database-initiated operation. How is the shell client and PHP access so different?
Reply content:
Start the Mongod server first;
Open a Shell client:
Use haha
Db.haha.insert ({' A ': ' AAA '})
Db.haha.find ()
{' A ': ' AAA '} //Added successfully
Open another shell client:
Use haha
Db.haha.find ()//confirm the input you just made
{' A ': ' AAA '} //is valid
As you can see, the shell client is effective.
Here is the PHP code:
$m = new Mongoclient ();
$jihe = $m->db->haha;
$cursor = $jihe->find ();
foreach ($cursor as $item) {
Var_dump ($item); Nothing, empty data
}
Then modify the PHP code to:
$m = new Mongoclient ();
$jihe = $m->db->haha;
$doc = Array (' b ' = ' BBB ');
$jihe->insert ($doc); PHP tries to insert the data itself
$cursor = $jihe->find ();
foreach ($cursor as $item) {
Var_dump ($item); This time only {' B ': ' BBB '} data, no shell set {' A ': ' AAA '}
}
What's going on here? is for the same database-initiated operation. How is the shell client and PHP access so different?
Speculation: The shell is connected to PHP, which is 2 of the haha
db
database space.
Open a Shell client, enter a command
show dbs
Output results In addition to Haha, is there a db? If yes
$jihe = $m->db->haha;
should read
$jihe = $m->haha->haha;
If it is no, (Bay stretched out) PHP has not touched for many years.
Make sure you're connected to the same server, the same database