Home > Backend Development > PHP Tutorial > 数据库 - PHP读写MongoDB,shell读写MongoDB,兵分两路操作,怎么数据还是相互隔离的?

数据库 - PHP读写MongoDB,shell读写MongoDB,兵分两路操作,怎么数据还是相互隔离的?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:47:22
Original
1148 people have browsed it

首先启动mongod服务器;
打开一个shell客户端:

use haha
db.haha.insert({'a':'aaa'})
db.haha.find()
{'a':'aaa'} // 添加成功了

打开另一个shell客户端:

use haha
db.haha.find() // 确认一下刚才的输入
{'a':'aaa'} // 果然有效

可以看到,shell客户端里是有效果的。
下面是php代码:

$m = new MongoClient();
$jihe = $m->db->haha;

$cursor = $jihe->find();
foreach($cursor as $item){
var_dump($item); //什么都没有,空数据
}

然后修改php代码为:

$m = new MongoClient();
$jihe = $m->db->haha;

$doc = array('b'=>'bbb');
$jihe->insert($doc); // php自己试着插入数据

$cursor = $jihe->find();
foreach($cursor as $item){
var_dump($item); //这次仅有 {'b':'bbb'}数据,没有shell里面设置的{'a':'aaa'}
}

这是怎么回事呢?都是针对同一个数据库发起的操作啊。shell客户端和php访问,怎么有如此差异?

回复内容:

首先启动mongod服务器;
打开一个shell客户端:

use haha
db.haha.insert({'a':'aaa'})
db.haha.find()
{'a':'aaa'} // 添加成功了

打开另一个shell客户端:

use haha
db.haha.find() // 确认一下刚才的输入
{'a':'aaa'} // 果然有效

可以看到,shell客户端里是有效果的。
下面是php代码:

$m = new MongoClient();
$jihe = $m->db->haha;

$cursor = $jihe->find();
foreach($cursor as $item){
var_dump($item); //什么都没有,空数据
}

然后修改php代码为:

$m = new MongoClient();
$jihe = $m->db->haha;

$doc = array('b'=>'bbb');
$jihe->insert($doc); // php自己试着插入数据

$cursor = $jihe->find();
foreach($cursor as $item){
var_dump($item); //这次仅有 {'b':'bbb'}数据,没有shell里面设置的{'a':'aaa'}
}

这是怎么回事呢?都是针对同一个数据库发起的操作啊。shell客户端和php访问,怎么有如此差异?

推测:shell连的是haha,php连的是db,这是2个数据库空间。
打开一个shell客户端,输入命令

<code>show dbs
</code>
Copy after login

输出结果除了haha,是不是还有一个db?如果是Yes

<code>$jihe = $m->db->haha;
</code>
Copy after login

应该改为

<code>$jihe = $m->haha->haha;
</code>
Copy after login

如果是No,(摊手)php多年没碰了。

确认你连的是同一个服务器,同一个数据库

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
mongodb start
From 1970-01-01 08:00:00
0
0
0
linux - ubuntu14 error installing mongodb
From 1970-01-01 08:00:00
0
0
0
Use of symfony2 mongodb
From 1970-01-01 08:00:00
0
0
0
mongodb _id rename
From 1970-01-01 08:00:00
0
0
0
Parameter understanding of mongodb
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template