-
- $conn = new Mongo ();
- can be shortened to
- $conn =new Mongo (); #连接本地主机, the default port.
- $conn =new Mongo ("172.21.15.69″"); #连接远程主机
- $conn =new Mongo ("Xiaocai.loc:10086″"); #连接指定端口远程主机
- $conn =new Mongo ("Xiaocai.loc", Array ("Replicaset" =>true)); #负载均衡
- $conn =new Mongo ("Xiaocai.loc", Array ("persist" = "T")); #持久连接
- $conn =new Mongo ("Mongodb://sa:123@localhost"); #带用户名密码
- #选择test数据库
- $db = $conn->test;
- $db = $conn->selectdb ("test"); #第二种写法
- #选择集合 (select "Table")
- $collection = $db->user;
- $collection = $db->selectcollection ("user"); #第二种写法
- #插入操作
- $data =array ("uid" = "zz123", "user_name" = "Zhang San");
- $result = $collection->insert ($data); #简单插入
- echo "Insert Data ID". $data ["_id"];
- Exit
- #插入操作 Safe Insertion
- $data =array ("uid" = "zz124", "user_name" = "John Doe");
- $result = $collection->insert ($data, true); #用于等待MongoDB完成操作 in order to determine whether it was successful. (This parameter is useful when you have a large number of records inserted)
- #修改操作
- $where =array ("uid" = "zz123");
- $newdata =array ("user_name" = "Zhang 33", "tel" and "123456789");
- $result = $collection->update ($where, Array (' $set ' = = $newdata));
- #替换更新
- $where =array ("uid" = "zz124");
- $newdata =array ("User_age" and "a", "tel" = "123456789");
- $result = $collection->update ($where, $newdata);
- #批量更新
- $where =array ("uid" = ' zz ');
- $newdata =array ("user_name" = "zz", "money" =>1000);
- $result = $collection->update ($where, Array (' $set ' = = $newdata), array (' multiple ' =>true));
- #自动累加
- $where =array (' money ' =>1000);
- $newdata =array (' user_name ' = ' edit ');
- $result = $collection->update ($where, Array (' $set ' = $newdata, ' $inc ' =>array (' money ' =>-5)));
- #删除节点
- $where =array (' uid ' = ' zz124 ');
- $result = $collection->update ($where, Array (' $unset ' = ' tel '));//Delete node Tel
- #删除数据
- $collection->remove (Array (' uid ' = ' zz124 '));
- #删除指定MongoId
- $id = new MongoId (' 4d638ea1d549a02801000011 ');
- $collection->remove (Array (' _id ' = = (object) $id));
- #查询数据 Note: $GT is greater than, $GTE is greater than or equal, $lt is less than, $lte is less than or equal, $ne is not equal, $exists does not exist
- Echo ' Count: '. $collection->count (). "
"; #全部
- Echo ' Count: '. $collection->count (' uid ' = ' zz123 '). "
"; #可以加上条件
- Echo ' Count: '. $collection->count (' Age ' =>array (' $gt ' =>10, ' $lte ' =>30)). "
"; #大于50小于等于74
- Echo ' Count: '. $collection->find ()->limit (5)->skip (0)->count (true). "
"; #获得实际返回的结果数
- #集合中所有文档
- $cursor = $collection->find ()->snapshot ();
- foreach ($cursor as $id = = $value) {
- echo "$id:"; Var_dump ($value);
- echo "
";
- }
- #查询一条数据
- $cursor = $collection->findone ();
- #排除列 false to not show
- $cursor = $collection->find ()->fields (Array ("Age" =>false, "tel" =>false));
- #指定列 true to display
- $cursor = $collection->find ()->fields (Array ("user_name" =>true));
- # (exists tel,age node) and age!=0 and age<50
- $where =array (' Tel ' =>array (' $exists ' =>true), ' Age ' =>array (' $ne ' =>0, ' $lt ' =>50, ' $exists ' =>true)) ;
- $cursor = $collection->find ($where);
- #分页获取结果集
- $cursor = $collection->find ()->limit (5)->skip (0);
- #排序
- $cursor = $collection->find ()->sort (Array (' Age ' =>-1, ' type ' =>1)); #1表示降序-1 means ascending, and the order of the arguments affects the sort sequence
- #索引
- $collection->ensureindex (Array (' age ' = 1, ' money ' =>-1)); #1表示降序-1 means ascending
- $collection->ensureindex (Array (' age ' = 1, ' money ' =>-1), array (' background ' =>true)); #索引的创建放在后台运行 (default is run synchronously)
- $collection->ensureindex (Array (' age ' = 1, ' money ' =>-1), array (' unique ' =>true)); #该索引是唯一的
- #取得查询结果
- $cursor = $collection->find ();
- $array =array ();
- foreach ($cursor as $id = = $value) {
- $array []= $value;
- }
- #关闭连接
- $conn->close ();
- ?>
Copy Code
|