国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MongoDB > 正文

mongodb 數(shù)據(jù)庫操作詳解--創(chuàng)建,切換,刪除

2020-10-29 18:53:48
字體:
來源:轉載
供稿:網(wǎng)友

mongodb安裝就不說了,請參考:centos yum 安裝 mongodb 以及php擴展

一,創(chuàng)建,切換,刪除數(shù)據(jù)庫

[root@localhost zhangy]# mongo MongoDB shell version: 2.4.6 connecting to: tank > use test      //創(chuàng)建 or 切換數(shù)據(jù)庫 switched to db test > db.dropDatabase()  //刪除數(shù)據(jù)庫 { "dropped" : "test", "ok" : 1 } 

二,php創(chuàng)建,切換,刪除數(shù)據(jù)庫

1,切換數(shù)據(jù)庫

$mongo = new Mongo(); $db = $mongo->selectDB('test');  //切換數(shù)據(jù)庫 

2,創(chuàng)建數(shù)據(jù)庫

$mongo = new Mongo(); $db = $mongo->selectDB('test'); $users = $db->createCollection("users");  $alldb = $mongo->listDBs(); //列出所有數(shù)據(jù)庫 print_r($alldb);      //可以看到db創(chuàng)建成功了 

在這里要注意一下,如果你不創(chuàng)建一個collection(根關系型數(shù)據(jù)庫的表基本上是一樣的),是創(chuàng)建不了數(shù)據(jù)庫的。

3,刪除數(shù)據(jù)庫

$mongo = new Mongo(); $db = $mongo->selectDB('test'); $db->drop(); 

三,小節(jié)

這篇文章很簡單吧,哈哈,不想在一篇文章里面寫太多的東西,折開來寫,看的更清楚一點,更細一點。
在這兒要提一下,mongodb命令下的幫助,這個對于命令行操作很有幫助。

1,db的幫助

db.AddUser(username,password) 添加用戶 db.auth(usrename,password)   設置數(shù)據(jù)庫連接驗證 db.cloneDataBase(fromhost)   從目標服務器克隆一個數(shù)據(jù)庫 db.commandHelp(name)      returns the help for the command db.copyDatabase(fromdb,todb,fromhost) 復制數(shù)據(jù)庫fromdb---源數(shù)據(jù)庫名稱,todb---目標數(shù)據(jù)庫名稱,fromhost---源數(shù)據(jù)庫服務器地址 db.createCollection(name,{size:3333,capped:333,max:88888}) 創(chuàng)建一個數(shù)據(jù)集,相當于一個表 db.currentOp()         取消當前庫的當前操作 db.dropDataBase()       刪除當前數(shù)據(jù)庫 db.eval(func,args)       run code server-side db.getCollection(cname)    取得一個數(shù)據(jù)集合,同用法:db['cname'] or db.getCollenctionNames()    取得所有數(shù)據(jù)集合的名稱列表 db.getLastError()       返回最后一個錯誤的提示消息 db.getLastErrorObj()      返回最后一個錯誤的對象 db.getMongo()         取得當前服務器的連接對象get the server db.getMondo().setSlaveOk()   allow this connection to read from then nonmaster membr of a replica pair db.getName()          返回當操作數(shù)據(jù)庫的名稱 db.getPrevError()       返回上一個錯誤對象 db.getProfilingLevel()     獲取profile level db.getReplicationInfo()    獲得重復的數(shù)據(jù) db.getSisterDB(name)      get the db at the same server as this onew db.killOp()          停止(殺死)在當前庫的當前操作 db.printCollectionStats()   返回當前庫的數(shù)據(jù)集狀態(tài) db.printReplicationInfo()    打印主數(shù)據(jù)庫的復制狀態(tài)信息 db.printSlaveReplicationInfo()    打印從數(shù)據(jù)庫的復制狀態(tài)信息 db.printShardingStatus()    返回當前數(shù)據(jù)庫是否為共享數(shù)據(jù)庫 db.removeUser(username)    刪除用戶 db.repairDatabase()      修復當前數(shù)據(jù)庫 db.resetError() db.runCommand(cmdObj)     run a database command. if cmdObj is a string, turns it into {cmdObj:1} db.setProfilingLevel(level)  設置profile level 0=off,1=slow,2=all db.shutdownServer()      關閉當前服務程序 db.version()          返回當前程序的版本信息 

2,表的幫助,格式,db.表名.help()

db.test.find({id:10})     返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集 db.test.find({id:10}).count() 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)總數(shù) db.test.find({id:10}).limit(2) 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集從第二條開始的數(shù)據(jù)集 db.test.find({id:10}).skip(8) 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集從0到第八條的數(shù)據(jù)集 db.test.find({id:10}).limit(2).skip(8) 返回test數(shù)據(jù)集ID=1=的數(shù)據(jù)集從第二條到第八條的數(shù)據(jù) db.test.find({id:10}).sort()  返回test數(shù)據(jù)集ID=10的排序數(shù)據(jù)集 db.test.findOne([query])    返回符合條件的一條數(shù)據(jù) db.test.getDB()        返回此數(shù)據(jù)集所屬的數(shù)據(jù)庫名稱 db.test.getIndexes()      返回些數(shù)據(jù)集的索引信息 db.test.group({key:...,initial:...,reduce:...[,cond:...]})  返回分組信息 db.test.mapReduce(mayFunction,reduceFunction,<optional params>) 這個有點像存儲過程 db.test.remove(query)           在數(shù)據(jù)集中刪除一條數(shù)據(jù) db.test.renameCollection(newName)     重命名些數(shù)據(jù)集名稱 db.test.save(obj)             往數(shù)據(jù)集中插入一條數(shù)據(jù) db.test.stats()              返回此數(shù)據(jù)集的狀態(tài) db.test.storageSize()           返回此數(shù)據(jù)集的存儲大小 db.test.totalIndexSize()          返回此數(shù)據(jù)集的索引文件大小 db.test.totalSize()            返回些數(shù)據(jù)集的總大小 db.test.update(query,object[,upsert_bool]) 在此數(shù)據(jù)集中更新一條數(shù)據(jù) db.test.validate()             驗證此數(shù)據(jù)集 db.test.getShardVersion()         返回數(shù)據(jù)集共享版本號 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 柯坪县| 灵璧县| 兴业县| 米林县| 大宁县| 太保市| 新宁县| 贵阳市| 册亨县| 武胜县| 棋牌| 嵊泗县| 沈丘县| 兴义市| 平果县| 亳州市| 平利县| 东兰县| 柘城县| 山西省| 通辽市| 剑河县| 辽宁省| 屏南县| 苗栗市| 垣曲县| 兴文县| 沅陵县| 哈巴河县| 黄冈市| 广汉市| 铁力市| 呼伦贝尔市| 云梦县| 义乌市| 加查县| 安吉县| 长葛市| 揭阳市| 南江县| 普格县|