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);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<?php // 连接到mongodb $mongoClient = new MongoClient(); // 选择一个数据库 $db = $mongoClient ->test; //获取集合 $collection = $db ->users; //更新文档 $condition = array (); $condition [ "id" ]=1; $data = array (); $data [ 'name' ]= "wangwu" ; $data [ 'age' ]= "11" ; $collection ->update( $condition , $data ); //插入文档 $data = array (); $data [ 'id' ]=4; $data [ 'name' ]= "哈哈" ; $data [ 'age' ]= "11" ; $collection ->insert( $data ); //删除文档 $condition = array (); $condition [ 'id' ]=2; $collection ->remove( $condition ); //查询文档 $users = $collection ->find(); foreach ( $users as $k => $v ) { print_r( $v ); } ?> |