标签:font div 资源 127.0.0.1 reac var nbsp 个数 pre
<?php //1.连接到MongoDB $host = "127.0.0.1"; $port = 27017; $server = "mongodb://$host:$port"; $mongodb = new MongoClient($server); echo "<pre>"; var_dump($mongodb); //2.选择数据库 $db = $client -> hytc; var_dump($db); //3.选择数据表 $collection = $db -> user; var_dump($collection);
注:库、表无需手动创建,选择后自动创建
insert(数据) 插入数据(必是一个数组)
//插入文档(关联数组) $data = ["name"=>"小明","age"=>10,"hobby"=>"swim"]; $result = $collection -> insert($data);
echo "<pre>";
var_dump($result);
find(查找条件,查找字段) 查询数据
//查询数据(返回的是cursor游标,资源) $condition = [‘name‘=>‘小明‘]; $fields = [ ‘_id‘ => false, //不查询id ‘name‘ => true, //查询name ‘hobby‘ => true //查询hobby ]; $result = $collection -> find($condition,$fields); foreach($result as $key => $value){ echo "<pre>"; var_dump($value); }
update(查找条件,更新之后的内容=>文档内容) 更新数据
//更新数据 $condition = [‘name‘=>‘小明‘]; $new_doc = [ ‘$set‘ => ["hobby" => "swim"] ]; $result = $collection -> update($condition,$new_doc); echo "<pre>"; var_dump($result);
注:
//更新数据 $condition = [‘age‘=>‘20‘]; $new_doc = [ ‘$set‘ => ["hobby" => "swim"] ]; $result = $collection -> update($condition,$new_doc,[‘multiple‘=>true]); echo "<pre>"; var_dump($result);
remove(查找条件) 删除数据
//删除数据 $condition = [‘name‘=>‘小明‘]; $result = $collection -> remove($condition);
标签:font div 资源 127.0.0.1 reac var nbsp 个数 pre
原文地址:https://www.cnblogs.com/writerW/p/8955137.html