Heim php教程 php手册 [MongoDB] mongodb与php - 陶士涵

[MongoDB] mongodb与php - 陶士涵

May 20, 2016 am 11:41 AM

windows上安装mongodb的php扩展

下载地址https://s3.amazonaws.com/drivers.mongodb.org/php/index.html

找到对应的php版本的dll文件,下载php_mongo.dll,放到php安装目录下的ext目录中,修改php.ini,添加一项extension=php_mongo.dll,没有找到支持php7的dll

 

 

 

获取MongoClient对象,new出来

获取数据库对象db,通过MongoClient对象的数据库属性,$MongoClient->数据库名

获取集合collection,通过db对象的集合属性,$db->集合名

创建集合,调用db对象的createCollection()方法,

调用collection对象的find()方法,查询数据,$collection->find()

调用collection对象的update ()方法,更新数据,$collection->update($condition,$data);

调用collection对象的insert ()方法,插入数据,$collection->insert($data);

 

<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 连接到mongodb</span>
<span style="color: #800080;">$mongoClient</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MongoClient();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 选择一个数据库</span>
<span style="color: #800080;">$db</span> = <span style="color: #800080;">$mongoClient</span>-><span style="color: #000000;">test;

</span><span style="color: #008000;">//</span><span style="color: #008000;">获取集合</span>
<span style="color: #800080;">$collection</span>=<span style="color: #800080;">$db</span>-><span style="color: #000000;">users;

</span><span style="color: #008000;">//</span><span style="color: #008000;">更新文档</span>
<span style="color: #800080;">$condition</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">();
</span><span style="color: #800080;">$condition</span>["id"]=1<span style="color: #000000;">;
</span><span style="color: #800080;">$data</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">();
</span><span style="color: #800080;">$data</span>['name']="wangwu"<span style="color: #000000;">;
</span><span style="color: #800080;">$data</span>['age']="11"<span style="color: #000000;">;
</span><span style="color: #800080;">$collection</span>->update(<span style="color: #800080;">$condition</span>,<span style="color: #800080;">$data</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;">插入文档</span>
<span style="color: #800080;">$data</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">();
</span><span style="color: #800080;">$data</span>['id']=4<span style="color: #000000;">;
</span><span style="color: #800080;">$data</span>['name']="哈哈"<span style="color: #000000;">;
</span><span style="color: #800080;">$data</span>['age']="11"<span style="color: #000000;">;
</span><span style="color: #800080;">$collection</span>->insert(<span style="color: #800080;">$data</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;">删除文档</span>
<span style="color: #800080;">$condition</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">();
</span><span style="color: #800080;">$condition</span>['id']=2<span style="color: #000000;">;
</span><span style="color: #800080;">$collection</span>->remove(<span style="color: #800080;">$condition</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;">查询文档</span>
<span style="color: #800080;">$users</span>=<span style="color: #800080;">$collection</span>-><span style="color: #000000;">find();
</span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$users</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$k</span> => <span style="color: #800080;">$v</span><span style="color: #000000;">) {
    </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$v</span><span style="color: #000000;">);
}
</span>?>
Nach dem Login kopieren

 

Erklärung dieser Website
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

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)