mongodb安裝就不說了,請參考:centos yum 安裝 mongodb 以及php擴充
一,建立,切換,刪除資料庫
[root@localhost zhangy]# mongo MongoDB shell version: 2.4.6 connecting to: tank > use test //建立 or 切換資料庫 switched to db test > db.dropDatabase() //刪除資料庫 { "dropped" : "test", "ok" : 1 }
二,php建立,切換,刪除資料庫
1,切換資料庫
$mongo = new Mongo(); $db = $mongo->selectDB('test'); //切換資料庫
2,建立資料庫
$mongo = new Mongo(); $db = $mongo->selectDB('test'); $users = $db->createCollection("users"); $alldb = $mongo->listDBs(); //列出所有資料庫 print_r($alldb); //可以看到db建立成功了
在這裡要注意一下,如果你不建立一個collection(根關係型資料庫的表基本上是一樣的),是建立不了資料庫的。
3,刪除資料庫
$mongo = new Mongo(); $db = $mongo->selectDB('test'); $db->drop();
三,小節
這篇文章很簡單吧,哈哈,不想在一篇文章裡面寫太多的東西,折開來寫,看的更清楚一點,更細一點。
在這兒要提一下,mongodb命令下的協助,這個對於命令列操作很有協助。
1,db的協助
db.AddUser(username,password) 添加使用者 db.auth(usrename,password) 設定資料庫連接驗證 db.cloneDataBase(fromhost) 從目標伺服器複製一個資料庫 db.commandHelp(name) returns the help for the command db.copyDatabase(fromdb,todb,fromhost) 複製資料庫fromdb---來源資料庫名稱,todb---目標資料庫名稱,fromhost---來源資料庫伺服器位址 db.createCollection(name,{size:3333,capped:333,max:88888}) 建立一個資料集,相當於一個表 db.currentOp() 取消當前庫的當前操作 db.dropDataBase() 刪除當前資料庫 db.eval(func,args) run code server-side db.getCollection(cname) 取得一個資料集合,同用法:db['cname'] or db.getCollenctionNames() 取得所有資料集合的名稱列表 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() 返回當操作資料庫的名稱 db.getPrevError() 返回上一個錯誤對象 db.getProfilingLevel() 擷取profile level db.getReplicationInfo() 獲得重複的資料 db.getSisterDB(name) get the db at the same server as this onew db.killOp() 停止(殺死)在當前庫的當前操作 db.printCollectionStats() 返回當前庫的資料集狀態 db.printReplicationInfo() 列印主要資料庫的複製狀態資訊 db.printSlaveReplicationInfo() 列印從資料庫的複製狀態資訊 db.printShardingStatus() 返回當前資料庫是否為共用資料庫 db.removeUser(username) 刪除使用者 db.repairDatabase() 修複當前資料庫 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資料集ID=10的資料集 db.test.find({id:10}).count() 返回test資料集ID=10的資料總數 db.test.find({id:10}).limit(2) 返回test資料集ID=10的資料集從第二條開始的資料集 db.test.find({id:10}).skip(8) 返回test資料集ID=10的資料集從0到第八條的資料集 db.test.find({id:10}).limit(2).skip(8) 返回test資料集ID=1=的資料集從第二條到第八條的資料 db.test.find({id:10}).sort() 返回test資料集ID=10的排序資料集 db.test.findOne([query]) 返回合格一條資料 db.test.getDB() 返回此資料集所屬的資料庫名稱 db.test.getIndexes() 返回些資料集的索引資訊 db.test.group({key:...,initial:...,reduce:...[,cond:...]}) 返回分組資訊 db.test.mapReduce(mayFunction,reduceFunction,<optional params>) 這個有點像預存程序 db.test.remove(query) 在資料集中刪除一條資料 db.test.renameCollection(newName) 重新命名些資料集名稱 db.test.save(obj) 往資料集中插入一條資料 db.test.stats() 返回此資料集的狀態 db.test.storageSize() 返回此資料集的儲存大小 db.test.totalIndexSize() 返回此資料集的索引檔案大小 db.test.totalSize() 返回些資料集的總大小 db.test.update(query,object[,upsert_bool]) 在此資料集中更新一條資料 db.test.validate() 驗證此資料集 db.test.getShardVersion() 返回資料集共用版本號碼