国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

MongoDB在PHP中的常用操作小結(jié)

2024-05-04 23:21:28
字體:
供稿:網(wǎng)友

$mongodb = new Mongo();

//$connection = new Mongo( "$dburl:$port" ); // connect to a remote host (default port)

$mydb = $mongodb->mydb;  //隱性創(chuàng)建數(shù)據(jù)庫mydb

$mydb = $mongodb->selectDB("mydb");  //直接選擇已經(jīng)存在的數(shù)據(jù)庫

$collection = $mydb->mycollect;   //選擇所用文集,如果不存在,自動創(chuàng)建

$collection = $db->selectCollection('mydb');   //只選擇,不創(chuàng)建

//插入新紀錄

$collection->insert(array("name"=>"l4yn3", "age"=>"10", "sex":"unknow"));


//修改記錄

$where = array("name"=>"l4yn3");

$update_item = array('$set'=>array("age"=>"15", "sex":"secret"));

$collection->update($where, $update_item);

$options['multiple'] = true; //默認是 false,是否改變匹配的多行

$collection->update($where, $update_item, $options);


//查詢記錄

$myinfo = $collection->findOne(array("name"=>"l4yn3"));

$myinfo = $collection->findOne(array("name"=>
"l4yn3"), array("age"=>"15"));


//按條件查找:
$query = array("name"=>"l4yn3");
$cursor = $collection->find($query); //在$collectio集合中查找滿足$query的文檔
while($cursor->hasNext())
{
var_dump($cursor->getNext()); //返回了數(shù)組
}


//返回文檔記錄數(shù)量

$collection->count();


//刪除一個數(shù)據(jù)庫:
$connection->dropDB("...");

//列出所有可用數(shù)據(jù)庫:
$m->listDBs(); //無返回值
//關(guān)閉連接:
$connection->close();

php各種連接mongodb數(shù)據(jù)庫的參數(shù)方式

//連接localhost:27017
$conn = new Mongo();
//連接遠程主機默認端口
$conn = new Mongo('test.com');
//連接遠程主機22011端口
$conn = new Mongo('test.com:22011');
//MongoDB有用戶名密碼
$conn = new Mongo("mongodb://${username}:${password}@localhost")
//MongoDB有用戶名密碼并指定數(shù)據(jù)庫blog
$conn = new Mongo("mongodb://${username}:${password}@localhost/blog");
//多個服務(wù)器
$conn = new Mongo("mongodb://localhost:27017,localhost:27018");

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 银川市| 睢宁县| 开化县| 连城县| 铅山县| 晋宁县| 巴东县| 宁乡县| 西华县| 济宁市| 墨玉县| 清水县| 府谷县| 三都| 大新县| 铁岭市| 交城县| 武强县| 桃源县| 吉林省| 峨眉山市| 当雄县| 化德县| 佳木斯市| 卢氏县| 拉萨市| 延川县| 海淀区| 微博| 郴州市| 泰州市| 荆州市| 麻江县| 三亚市| 丰镇市| 南华县| 吉安市| 古田县| 长海县| 龙山县| 尤溪县|