mongodb 數(shù)據(jù)庫操作詳解--創(chuà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ù)集共享版本號
相關文章
mongodb中ObjectId和ObjectIdr實現(xiàn)
本文主要介紹了mongodb中ObjectId和ObjectIdr實現(xiàn)2023-12-12批量備份還原導入與導出MongoDB數(shù)據(jù)方式
前面我們介紹了mongoDB的安裝與配置,接下來我們講一講如何使用mongo的可視化工具Navicat對mongo數(shù)據(jù)庫進行操作和如何在終端進行mongo數(shù)據(jù)庫對數(shù)據(jù)的批量操作2021-09-09MongoDB中的主從同步配置和mongod相關啟動命令講解
這篇文章主要介紹了MongoDB中的主從同步配置和mongod相關啟動命令講解,文中也羅列了很多其他常用的mongod命令參數(shù),需要的朋友可以參考下2016-03-03Pycharm連接MongoDB數(shù)據(jù)庫安裝教程詳解
這篇文章主要介紹了Pycharm連接MongoDB數(shù)據(jù)庫安裝教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11詳解Mongodb?多文檔聚合操作處理方法(Map-reduce?函數(shù))
這篇文章主要介紹了Mongodb多文檔聚合操作處理方法(Map-reduce函數(shù)),本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07db.serverStatus()命名執(zhí)行時報無權(quán)限問題的解決方法
這篇文章主要給大家介紹了關于db.serverStatus()命名執(zhí)行時報無權(quán)限問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-07-07MongoDB中優(yōu)雅刪除大量數(shù)據(jù)的三種方式
最近接到一個任務,線上的mongodb積累了大量的無用數(shù)據(jù),導致宕機,現(xiàn)在對里面的數(shù)據(jù)進行批量刪除,所以這篇文章主要給大家介紹了關于MongoDB中優(yōu)雅刪除大量數(shù)據(jù)的三種方式,需要的朋友可以參考下2021-10-10