安裝後在控制台輸入:
mongo啟動用戶端。
show dbs顯示所有資料庫。
use xxx選中資料庫為當前使用的資料庫。
show collections顯示當前資料庫的所有集合。
db.colleciton.find()來尋找表格的所有資料。
db.serverStatus()查看資料庫伺服器的狀態。
db.stats()查詢指定資料庫的統計資訊。
use xxx如果資料庫不存在則建立資料庫(必須在雖然插入一條資料)
db.storeCollection.insert({'version':'13', 'segment':'456'})插入資料,同時建立集合
db.dropDatabase()刪除資料庫
db.mycoll.drop()刪除集合
db.storeCollection.save({'version':'3.5', 'segment':'e3ol6'})更新記錄
db.storeCollection.remove({'version':'3.5'}) 刪除記錄
db.page.ensureIndex({'title':1, 'url':-1})建立索引,1正序,-1逆序
db.page.getIndexes()查詢建立的索引
db.mycoll.dropIndex(name) 刪除索引,如果不帶參數,刪除所有索引
db.baseSe.reIndex()重建索引
db.baseSe.storageSize()查詢指定資料庫的集合的可用的儲存空間
db.baseSe.totalSize()查詢集合已指派的儲存空間
PS:非正常關閉後無法啟動問題
段時間不小心非正常關閉了mongoDB,準備重啟卻發現不行,錯誤資訊如下:
Tue Apr 17 11:13:30Tue Apr 17 11:13:30 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.Tue Apr 17 11:13:30Tue Apr 17 11:13:30 [initandlisten] MongoDB starting : pid=7128 port=27017 dbpath=D:\Programs\mongodb204\db 32-bit host=FALCONTue Apr 17 11:13:30 [initandlisten]Tue Apr 17 11:13:30 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of dataTue Apr 17 11:13:30 [initandlisten] ** see http://blog.mongodb.org/post/137788967/32-bit-limitationsTue Apr 17 11:13:30 [initandlisten] ** with --journal, the limit is lowerTue Apr 17 11:13:30 [initandlisten]Tue Apr 17 11:13:30 [initandlisten] db version v2.0.4, pdfile version 4.5Tue Apr 17 11:13:30 [initandlisten] git version: 329f3c47fe8136c03392c8f0e548506cb21f8ebfTue Apr 17 11:13:30 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, service_pack='Service Pack 2')BOOST_LIB_VERSION=1_42Tue Apr 17 11:13:30 [initandlisten] options: { dbpath: "D:\Programs\mongodb204\db" }**************Unclean shutdown detected.Please visit http://dochub.mongodb.org/core/repair for recovery instructions.*************Tue Apr 17 11:13:30 [initandlisten] exception in initAndListen: 12596 old lock file, terminatingTue Apr 17 11:13:30 dbexit:Tue Apr 17 11:13:30 [initandlisten] shutdown: going to close listening sockets...Tue Apr 17 11:13:30 [initandlisten] shutdown: going to flush diaglog...Tue Apr 17 11:13:30 [initandlisten] shutdown: going to close sockets...Tue Apr 17 11:13:30 [initandlisten] shutdown: waiting for fs preallocator...Tue Apr 17 11:13:30 [initandlisten] shutdown: closing all files...Tue Apr 17 11:13:30 [initandlisten] closeAllFiles() finishedTue Apr 17 11:13:30 dbexit: really exiting now
解決方案:
1、刪除%MONGO_HOME%/db下的.lock檔案
2、輸入命令 mongod --repair
3、重啟mongoDB