Database-PHP reads and writes MongoDB, and shell reads and writes MongoDB. How are data still isolated from each other? -Php tutorial

Source: Internet
Author: User
Start the mongod server first, and open a shell client: usehahadb. haha. insert ({& #039; a & #039;: & #039; aaa & #039;}) db. haha. find () {& #039; a & #039;: & #039; aaa & #039;} added successfully to open another shell client: usehahadb. haha. find () confirm the input {& #039; a & #039 ;:& #039 ;... 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.
{'A': 'aaa '}// Valid indeed

It can be seen that the shell client has an effect.
The following is the php code:

$ M = new clients client ();
$ Jihe = $ m-> db-> haha;

$ Cursor = $ jihe-> find ();
Foreach ($ cursor as $ item ){
Var_dump ($ item); // nothing, empty data
}

Then modify the php code:

$ M = new clients client ();
$ Jihe = $ m-> db-> haha;

$ Doc = array ('B' => 'bbb ');
$ Jihe-> insert ($ doc );// Php tries to insert data.

$ Cursor = $ jihe-> find ();
Foreach ($ cursor as $ item ){
Var_dump ($ item); // this time only{'B': 'bbb '}Data, not set in shell{'A': 'aaa '}
}

What's going on? All operations initiated for the same database. How is the difference between shell client access and php access?

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.
{'A': 'aaa '}// Valid indeed

It can be seen that the shell client has an effect.
The following is the php code:

$ M = new clients client ();
$ Jihe = $ m-> db-> haha;

$ Cursor = $ jihe-> find ();
Foreach ($ cursor as $ item ){
Var_dump ($ item); // nothing, empty data
}

Then modify the php code:

$ M = new clients client ();
$ Jihe = $ m-> db-> haha;

$ Doc = array ('B' => 'bbb ');
$ Jihe-> insert ($ doc );// Php tries to insert data.

$ Cursor = $ jihe-> find ();
Foreach ($ cursor as $ item ){
Var_dump ($ item); // this time only{'B': 'bbb '}Data, not set in shell{'A': 'aaa '}
}

What's going on? All operations initiated for the same database. How is the difference between shell client access and php access?

Speculation: shell is connectedhahaPhp is connecteddbThis is Two database spaces.
Open a shell client and enter the command

show dbs

Besides haha, is there a db? If Yes

$jihe = $m->db->haha;

Should be changed

$jihe = $m->haha->haha;

If it is No, (vendors) php has not been touched for many years.

Make sure you are connected to the same server and database

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.