Database-PHP Read-write Mongodb,shell read and write MongoDB, the two operations, how the data is isolated from each other?

Source: Internet
Author: User
Tags mongoclient
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

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.