Heim  >  Artikel  >  Datenbank  >  mongodb insert数据 ,php扩展 insert 数据

mongodb insert数据 ,php扩展 insert 数据

WBOY
WBOYOriginal
2016-06-07 16:37:051386Durchsuche

前面说了到数据库连接操作,请参考: mongodb 用户 权限 设置 详解 对数据库的操作:请参考: mongodb 数据库创建,切换,删除 下面说一下,数据库表的插入操作 1,命令行下的insert操作 use test; #切换到test数据库switched to db test document=({"title" :

前面说了到数据库连接操作,请参考:mongodb 用户 权限 设置 详解

对数据库的操作:请参考:mongodb 数据库创建,切换,删除

下面说一下,数据库表的插入操作

1,命令行下的insert操作

> use test;                #切换到test数据库
switched to db test
> document=({"title" : "linux命令", "auther" : "tank" });         #定义了一个变量
{ "title" : "linux命令", "auther" : "tank" }
> db.test.insert(document);                 #插入变量
> db.test.find();                           #查看插入的数据
{ "_id" : ObjectId("53c8fc1cf062ac30ee8b9d2d"), "title" : "linux命令", "auther" : "tank" }
> db.test.insert({"title" : "51yip", "auther" : "tank" });        #直接插入数据
> db.test.find();                           #查看
{ "_id" : ObjectId("53c8fc1cf062ac30ee8b9d2d"), "title" : "linux命令", "auther" : "tank" }
{ "_id" : ObjectId("53c8f6fff062ac30ee8b9d2e"), "title" : "51yip", "auther" : "tank" }

2,利用php扩展insert数据

<?php //$mongo = new Mongo("mongodb://192.168.10.202:27017");    //链接远程数据库
$mongo = new Mongo();                                      //链接远程数据库
$curDB = $mongo->selectDB("test");               //选择要操作的数据库,如果不存在,则自动创建
$collection = $curDB->selectCollection("test");  //选中一个集合(理解为 table),如果不存在,则自动创建
//$collection->drop();                           //清空集合 testCollection 
$count = $collection->count();                   //查看集合中的数据量
echo "insert前集合中有[".$count."]条数据";     //这里的二条数据主命令行下插入的。
echo "********** mongodb php insert 插入 *************";
$obj = array("title"=>"围城","auther"=>"钱钟书");
$rel = $collection->insert($obj);
var_dump($rel);                                  //打印插入后的结果是bool型的
echo "新增对象的id:".$obj['_id']."";
$obj = array("title"=>"朝发白帝城","auther"=>"李白");
$rel = $collection->insert($obj,array('safe'=>true)); //safe 表示是否返回操作结果信息,返回的信息为 array
print_r($rel);                                   //插入后的结果是数组
echo "新增对象的id:".$obj['_id']."";;
$count = $collection->count();                  //查看集合中的数据量
echo "insert后集合中有[".$count."]条数据";
?>
运行结果:
insert前集合中有[2]条数据
********** mongodb php insert 插入 *************
bool(true)
新增对象的id:53c908c87f8b9ad7218b4568
Array ( [n] => 0 [connectionId] => 4 [err] => [ok] => 1 )
新增对象的id:53c908c87f8b9ad7218b4569
insert后集合中有[4]条数据
mongodb insert数据 ,php扩展 insert 数据 前面说了到数据库连接操作,请参考:mongodb 用户 权限 设置 详解 对数据库的操作:请参考:mongodb 数据库创建,切换,删除 下面说一下,数据库表的插入操作 1,命令行下的insert操作 > use test; #切换到test数据库 switched to db test > document=({"title" : "linux命令", "auther" : "tank" }); #定义了一个变量 { "title" : "linux命令", "auther" : "tank" } > db.test.insert(document); [...]mongodb insert数据 ,php扩展 insert 数据
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn