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

[MongoDB] mongodb与php - 陶士涵

WBOY
Freigeben: 2016-05-20 11:41:00
Original
958 Leute haben es durchsucht

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

 

Verwandte Etiketten:
Quelle:php.cn
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
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage