Home 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>?>
Copy after login

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)