php 原生查询mongo,PHP操作MongoDB的原生CURD方法
//連接數(shù)據(jù)庫
$m?=?new?MongoClient("mongodb://127.0.0.1:27017");?//?連接默認(rèn)主機和端口為:mongodb://localhost:27017
//$m?=?new?MongoClient("mongodb://root_mongo:xtwvfwlgr47782906@127.0.0.1:27017");??//連接設(shè)置了權(quán)限的mongo
//$m?=?new?MongoClient(?"mongodb://example.com"?);?//?連接遠(yuǎn)程數(shù)據(jù)庫,默認(rèn)端口為27017
//$m?=?new?MongoClient(?"mongodb://example.com:65432"?);?//?連接遠(yuǎn)程數(shù)據(jù)庫,端口號為指定的端口號。
$db?=?$m->form1;?//?獲取名稱為?"form1"?的數(shù)據(jù)庫
//創(chuàng)建集合
$collection?=?$db->createCollection("runoob");
echo?"集合創(chuàng)建成功";
//插入文檔
$collection?=?$db->form1;?//?選擇集合
$document?=?array(
"title"?=>?"MongoDB",
"description"?=>?"database",
"likes"?=>?100,
"url"?=>?"http://www.runoob.com/mongodb/",
"by",?"form1"
);
$collection->insert($document);
echo?"數(shù)據(jù)插入成功";
//?更新文檔?找到的第一條
$collection->update(array("title"=>"MongoDB"),?array('$set'=>array("title"=>"MongoDB?教程")));
//?更新文檔?找到的所有條
$collection->update(
array("title"=>"MongoDB"),
array('$set'=>array("title"=>"MongoDB?教程")),
array('multiple'?=>?true)
);
//查找文檔
$cursor?=?$collection->find();
var_dump($cursor);
//?迭代顯示文檔標(biāo)題
foreach?($cursor?as?$document)?{
echo?$document["title"]?.?"
";
}
//?移除文檔
$collection->remove(array("title"=>"MongoDB?教程"),?array("justOne"?=>?true));
除了以上實例外,在php中你還可以使用findOne(), save(), limit(), skip(), sort()等方法來操作Mongodb數(shù)據(jù)庫。
更多的操作方法可以參考 Mongodb 核心類:http://php.net/manual/zh/mongo.core.php。
總結(jié)
以上是生活随笔為你收集整理的php 原生查询mongo,PHP操作MongoDB的原生CURD方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 被一首歌折磨疯了,求助万能天涯?[已扎口
- 下一篇: 进来再说吧