一、安装
1、windows下的安装
下载地址:http://dl.mongodb.org/dl/win32
添加mongo到服务中:
开启服务:
启动文件:
::表示在此语句后所有运行的命令都不显示命令行本身 ::进入G盘 G: cd G:phpstudymongodbin mongod --dbpath "G:phpstudymongodbdata"
2、linux下的安装
待续。。。
二、shell命令
待续。。。
三、可视化工具
链接:https://pan.baidu.com/s/1uTsIVbOAJeSh68uk_Jg1fg
提取码:s9tb
Studio 3T 破解教程:
1、创建文件studio3t.bat
@echo off ECHO 重置Studio 3T的使用日期...... FOR /f "tokens=1,2,* " %%i IN ('reg query "HKEY_CURRENT_USERSoftwareJavaSoftPrefs3tmongochefenterprise" ^| find /V "installation" ^| find /V "HKEY"') DO ECHO yes | reg add "HKEY_CURRENT_USERSoftwareJavaSoftPrefs3tmongochefenterprise" /v %%i /t REG_SZ /d "" ECHO 重置完成, 按任意键退出...... pause>nul exit
2、将文件studio3t.bat文件移动到如下路径中
C:ProgramDataMicrosoftWindowsStart MenuProgramsStartUp
3、双击运行
然后运行studio 3t软件
四、PHP操作mongodb
SQL查询语句 | Mongo查询语句 |
---|---|
CREATE TABLE USERS (a Number, b Number) | 隐式的创建,或 MongoDB::createCollection(). |
INSERT INTO USERS VALUES(1,1) | $db->users->insert(array("a" => 1, "b" => 1)); |
SELECT a,b FROM users | $db->users->find(array(), array("a" => 1, "b" => 1)); |
SELECT * FROM users WHERE age=33 | $db->users->find(array("age" => 33)); |
SELECT a,b FROM users WHERE age=33 | $db->users->find(array("age" => 33), array("a" => 1, "b" => 1)); |
SELECT a,b FROM users WHERE age=33 ORDER BY name | $db->users->find(array("age" => 33), array("a" => 1, "b" => 1))->sort(array("name" => 1)); |
SELECT * FROM users WHERE age>33 | $db->users->find(array("age" => array('$gt' => 33))); |
SELECT * FROM users WHERE age<33 | $db->users->find(array("age" => array('$lt' => 33))); |
SELECT * FROM users WHERE name LIKE "%Joe%" | $db->users->find(array("name" => new MongoRegex("/Joe/"))); |
SELECT * FROM users WHERE name LIKE "Joe%" | $db->users->find(array("name" => new MongoRegex("/^Joe/"))); |
SELECT * FROM users WHERE age>33 AND age<=40 | $db->users->find(array("age" => array('$gt' => 33, '$lte' => 40))); |
SELECT * FROM users ORDER BY name DESC | $db->users->find()->sort(array("name" => -1)); |
CREATE INDEX myindexname ON users(name) | $db->users->ensureIndex(array("name" => 1)); |
CREATE INDEX myindexname ON users(name,ts DESC) | $db->users->ensureIndex(array("name" => 1, "ts" => -1)); |
SELECT * FROM users WHERE a=1 and b='q' | $db->users->find(array("a" => 1, "b" => "q")); |
SELECT * FROM users LIMIT 20, 10 | $db->users->find()->limit(10)->skip(20); |
SELECT * FROM users WHERE a=1 or b=2 | $db->users->find(array('$or' => array(array("a" => 1), array("b" => 2)))); |
SELECT * FROM users LIMIT 1 | $db->users->find()->limit(1); |
EXPLAIN SELECT * FROM users WHERE z=3 | $db->users->find(array("z" => 3))->explain() |
SELECT DISTINCT last_name FROM users | $db->command(array("distinct" => "users", "key" => "last_name")); |
SELECT COUNT(*y) FROM users | $db->users->count(); |
SELECT COUNT(*y) FROM users where AGE > 30 | $db->users->find(array("age" => array('$gt' => 30)))->count(); |
SELECT COUNT(AGE) from users | $db->users->find(array("age" => array('$exists' => true)))->count(); |
UPDATE users SET a=1 WHERE b='q' | $db->users->update(array("b" => "q"), array('$set' => array("a" => 1))); |
UPDATE users SET a=a+2 WHERE b='q' | $db->users->update(array("b" => "q"), array('$inc' => array("a" => 2))); |
DELETE FROM users WHERE z="abc" | $db->users->remove(array("z" => "abc")); |
$doc = [//定义一个文档,即一个数组 'First Name' => 'Yang', 'Last Name' => 'Yang', 'Age' => 21, 'Phone' => '110', 'Address' => [ 'Country' => 'China', 'City' => 'Shen Zhen' ], 'E-Mail' => [ '123456@qq.com', ] ];
1、插入数据
$mongo = new MongoClient('mongodb://localhost:27017'); //选择数据库 $db = $mongo->selectDB("test"); //选择集合 $collection = $db->selectCollection("admin"); //新增 $res = $collection->insert(array("name" => "yangs")); var_dump($res); //array(4) { ["n"]=> int(0) ["ok"]=> float(1) 插入成功
2、查询数据
a、查询单个数据
$data = $collection->findOne(array("name" => "yangs")); var_dump($data); //array(4) { ["_id"]=> object(MongoId)#6 (1) { ["$id"]=> string(24) "5d5f9f51146e638c0600002c" } ["name"]=> string(5) "yangs" ["age"]=> int(18) ["like"]=> array(2) { [0]=> string(9) "玩游戏" [1]=> string(9) "打篮球" } }
b、查询多个数据
//查询所有 $data = $collection->find(); foreach ($data as $k => $v) { var_dump($v); }
c、条件查询
//条件查询 //1、mongodb分别使用$lt、$lte、$eq、$gte、$gt、$ne表示<、<=、=、>=、>、<>,用于整数字段查询 $cursor = $collection->find(array("age" =>array('$gt' =>10))); while ($doc = $cursor->getNext()){ var_dump($doc); } //2、$in:匹配多个值中任意一个 $cursor = $collection->find(array("Address.Country" => array('$in' => array("China", "USA")))); while ($doc = $cursor->getNext()){ var_dump($doc); } //3、$all:匹配多个值中所有值(用于数组字段查询) $cursor = $collection->find(array("E-Mail" => array('$all' => array("123456@qq.com", "abc@qq.com")))); while ($doc = $cursor->getNext()){ var_dump($doc);//空 } //3、$or:或查询 $cursor = $collection->find(array('$or'=>array(array("Address.Country" => "China"), array("Address.Country" => "USA")))); while ($doc = $cursor->getNext()){ var_dump($doc); } //4、$slice:获取数组字段中指定数目的元素,位于find()函数第二个参数中 $cursor = $collection->find(['First Name' => 'Yang'], ['E-Mail' => ['$slice' => 2]]);//只返回前两个email $cursor = $collection->find(['First Name' => 'Yang'], ['E-Mail' => ['$slice' => -2]]);//只返回最后两个email $cursor = $collection->find(['First Name' => 'Yang'], ['E-Mail' => ['$slice' => [1, 2]]]);//忽略第一个,返回接下来两个 //5、$exists:根据某个字段是否有设置值进行查询 $cursor = $collection->find(['Hobby' => ['$exists' => false]]);//查找Hobby字段未设置值的文档 //6、正则表达式查询 $cursor = $collection->find(['First Name' => new MongoRegex('/^Ya/i')]);//查找First Name字段以Ya开头的文档,忽略大小写差异
d、聚集查询
//聚合查询 $data = $collection->aggregate(array( '$group' => array( "_id" => "Address.Country", "total" => array('$sum' => 1),//求总和,表示每匹配一个文档总和就加1 "minAge" => array('$min' => '$Age'), //分组中Age字段最小值 ) ), [ "cursor" => [ "batchSize" => 0 ] ]); $res = $collection->aggregate([ [//过滤条件:只对符合条件的原始文档进行聚合运算,若是放在'$group'之后则是只返回符合条件的结果文档 '$match' => ['Age' => ['$gt' => 30]] ], [//指定分组字段、统计字段 '$group' => [ '_id' => '$Address.Country', 'totalAge' => ['$sum' => '$Age']//计算各个分组Age字段总和 ] ], //以下操作若是放在'$group'之前则在聚合前作用于原始文档,若放在'$group'之后则在聚合后作用于结果文档 ['$unwind' => '$E-Mail'],//将包含有某个数组类型字段的文档拆分成多个文档,每个文档的同名字段的值为数组中的一个值。 ['$project' => ['myAge' => '$Age', 'First Name' => '$First Name']],//指定返回字段,可以对字段进行重命名,格式:返回字段名 => $原来字段名 ['$skip' => 2],//跳过指定数量的文档 ['$limit' => 2],//只返回指定数量的文档 ['$sort' => ['totalAge' => 1]]//排序 ], [ "cursor" => [ "batchSize" => 0 ] ]);
3、修改
//$set:重置特定键的值,若字段不存在则新建字段并赋值 $res = $collection->update(['First Name' => 'Yang'], ['$set' => ['Hobby' => 'pingpong']]); //$unset:删除字段 $res = $collection->update(['First Name' => 'Yang'], ['$unset' => ['Hobby' => 1]]); //$rename:重命名字段,若字段不存在则不进行任何操作 $res = $collection->update(['First Name' => 'Yang'], ['$rename' => ['Hobby' => 'hobby', 'Age' => 'age']]); //注意:如果文档中已经使用了指定名称的字段,则该字段将会被删除,然后再进行重命名操作。 //$setOnInsert:设置了upsert为true,并且发生了插入操作的时候,将某个字段设置为特定的 $res = $collection->update(['First Name' => 'Yang'], ['$setOnInsert' => ['lang' => 'English']], ['upsert' => true]); //$push:向指定字段添加一个值(作用于数组字段),若字段不存在会先创建字段,若字段值不是数组会报错 $res = $collection->update(['First Name' => 'Yang'], ['$push' => ['E-Mail' => '123123@qq.com']]); //$push:向指定字段添加多个值(作用于数组字段),若字段不存在会先创建字段,若字段值不是数组会报错 $res = $collection->update(['First Name' => 'Yang'], ['$pushAll' => ['E-Mail' => ['666@qq.com', '8888888@qq.com']]]); //使用$push和$each向某个字段添加多个值(作用于数组字段),若字段不存在会先创建字段,若字段值不是数组会报错 $res = $collection->update(['First Name' => 'Yang'], ['$push' => ['E-Mail' => ['$each' => ['123123@qq.com', '666@qq.com']]]]); //$addToSet:将数据添加到数组中(只在目标数组没有该数据的时候才将数据添加到数组中) $res = $collection->update(['First Name' => 'Yang'], ['$addToSet' => ['E-Mail' => '123123@qq.com']]); $res = $collection->update(['First Name' => 'Yang'], ['$addToSet' => ['E-Mail' => ['$each' => ['123123@qq.com', '666@qq.com']]]]); //$pop:从数组中删除一个元素,-1表示删除第一个元素,1表示删除最后一个元素(其实负数都删除第一个元素,0或正数都删除最后一个元素) $res = $collection->update(['First Name' => 'Yang'], ['$pop' => ['E-Mail' => 1]]); //$pull:删除数组中所有指定值 $res = $collection->update(['First Name' => 'Yang'], ['$pull' => ['E-Mail' => '123123@qq.com']]); //$pullAll:删除数组中多个元素的所有值 $res = $collection->update(['First Name' => 'Yang'], ['$pullAll' => ['E-Mail' => ['123123@qq.com', '666@qq.com']]]); //save() //参数1:希望保存的信息数组 //参数2:扩展选项 // fsync:若设置为true,w参数将被覆盖为0,数据将在更新结果返回前同步到磁盘。 // w:默认为1;若设置为0,更新操作将不会得到确认;使用复制集时可设置为n,确保主服务器在将修改复制到n个节点后才确认该更新操作 // j:默认为false,若设置为true,数据将在更新结果返回之前写入到日志中。 // wtimeout:默认为10000(毫秒),用于指定服务器等待接收确认的时间 // timeout:指定客户端需要等待服务器响应的超时时间(毫秒) //注意:若已存在则更新,若不存在则插入;更新时使用参数1指定的信息数组替换整个文档。 //若想更新则应该在参数1中指定_id键的值。 $doc = [//定义一个文档,即一个数组 'First Name' => 'y', 'Last Name' => 'y', 'Age' => 18, 'Phone' => '112', 'Address' => [ 'Country' => 'China', 'City' => '成都' ], 'E-Mail' => [ 'abc@qq.com', ] ]; //$res['ok']=1表示操作成功,$res['updatedExisting']=1表示更新,$res['upserted']=1表示插入 $res = $collection->save($doc); //findAndModify() //参数1:指定查询条件 //参数2:指定用于更新文档的信息 //参数3:可选,指定希望返回的字段 //参数4:扩展选项 // sort:以特定顺序对匹配文档进行排序 // remove:若设置为true,第一个匹配文档将被删除 // update:若设置为true,将在被选择的文档上执行更新操作 // new:默认为false,若设置为true则返回更新后的文档,否则返回更新前的文档 // upsert:若设置为true,没有找到匹配文档的时候将插入一个新的文档 $res = $collection->findAndModify(['First Name' => 'y'], ['$push' => ['E-Mail' => '123@qq.com']]);
4、删除
$res = $collection->remove(array("name" => "admin"));//$res['n']表示删除了几个文档
五、PHP7以上操作Mongodb
1、查询
$manager = new MongoDBDriverManager('mongodb://localhost:27017'); $query = new MongoDBDriverQuery(['age' => 24], ['sort' => ['age' => 1]]); $cursor = $manager->executeQuery('wjt.friend', $query); $data = []; foreach($cursor as $doc) { $data[] = $doc; }
2、添加
$manager = new MongoDBDriverManager('mongodb://localhost:27017'); $bulk = new MongoDBDriverBulkWrite; $bulk->insert(['name' => 'JetWu5', 'age' => 26]); $bulk->insert(['name' => 'JetWu6', 'age' => 26]); $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);//可选,修改确认 $res = $manager->executeBulkWrite('wjt.friend', $bulk, $writeConcern);
3、修改
$manager = new MongoDBDriverManager('mongodb://localhost:27017'); $bulk = new MongoDBDriverBulkWrite; $bulk->update( ['name' => 'JetWu5'], ['$set' => ['age' => 30, 'promise' => 'always smile!']] ); $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);//可选,修改确认 $res = $manager->executeBulkWrite('wjt.friend', $bulk, $writeConcern); echo '<pre>'; print_r($res);
4、删除
$manager = new MongoDBDriverManager('mongodb://localhost:27017'); $bulk = new MongoDBDriverBulkWrite; $bulk->delete(['name' => 'JetWu3']); $bulk->delete(['name' => 'JetWu4']); $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);//可选,修改确认 $res = $manager->executeBulkWrite('wjt.friend', $bulk, $writeConcern); echo '<pre>'; print_r($res);