Heim  >  Artikel  >  Backend-Entwicklung  >  [MongoDB] mongodb与php,mongodbphp_PHP教程

[MongoDB] mongodb与php,mongodbphp_PHP教程

WBOY
WBOYOriginal
2016-07-12 08:54:01902Durchsuche

[MongoDB] mongodb与php,mongodbphp

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>php
</span><span>//</span><span> 连接到mongodb</span>
<span>$mongoClient</span> = <span>new</span><span> MongoClient();
</span><span>//</span><span> 选择一个数据库</span>
<span>$db</span> = <span>$mongoClient</span>-><span>test;

</span><span>//</span><span>获取集合</span>
<span>$collection</span>=<span>$db</span>-><span>users;

</span><span>//</span><span>更新文档</span>
<span>$condition</span>=<span>array</span><span>();
</span><span>$condition</span>["id"]=1<span>;
</span><span>$data</span>=<span>array</span><span>();
</span><span>$data</span>['name']="wangwu"<span>;
</span><span>$data</span>['age']="11"<span>;
</span><span>$collection</span>->update(<span>$condition</span>,<span>$data</span><span>);

</span><span>//</span><span>插入文档</span>
<span>$data</span>=<span>array</span><span>();
</span><span>$data</span>['id']=4<span>;
</span><span>$data</span>['name']="哈哈"<span>;
</span><span>$data</span>['age']="11"<span>;
</span><span>$collection</span>->insert(<span>$data</span><span>);

</span><span>//</span><span>删除文档</span>
<span>$condition</span>=<span>array</span><span>();
</span><span>$condition</span>['id']=2<span>;
</span><span>$collection</span>->remove(<span>$condition</span><span>);

</span><span>//</span><span>查询文档</span>
<span>$users</span>=<span>$collection</span>-><span>find();
</span><span>foreach</span> (<span>$users</span> <span>as</span> <span>$k</span> => <span>$v</span><span>) {
    </span><span>print_r</span>(<span>$v</span><span>);
}
</span>?>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1122396.htmlTechArticle[MongoDB] mongodb与php,mongodbphp windows上安装mongodb的php扩展 下载地址https://s3.amazonaws.com/drivers.mongodb.org/php/index.html 找到对应的php版本的dll文件,...
Stellungnahme:
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