標籤:tor   博文   name   ssi   print   查看   pretty   question   start   
本博文介紹了MongoDB,並詳細指引讀者在Ubuntu下MongoDB的安裝和使用。本教程在Ubuntu14.04下測試通過.(2017.09.07)
 
安裝MongoDB
MongoDB安裝很簡單,無需下載源檔案,可以直接用apt-get命令進行安裝。 
開啟終端,輸入以下命令:
sudo apt-get install mongodb
安裝完成後,在終端輸入以下命令查看MongoDB版本:
mongo -version
輸出版本資訊,表明安裝成功,如下:
[email protected]:/usr/src# mongo -version
MongoDB shell version: 2.4.9
啟動和關閉mongodb命令如下:
service mongodb start
輸入命令,結果如下:
[email protected]:/usr/src# service mongodb start
start: Job is already running: mongodb
service mongodb stop
預設設定MongoDB是隨Ubuntu啟動自動啟動的。 
輸入以下命令查看是否啟動成功:
pgrep mongo -l   #注意:-l是英文字母l,不是阿拉伯數字1
卸載MongoDB
sudo apt-get --purge remove mongodb mongodb-clients mongodb-server
使用MongoDB
 
shell命令模式
輸入mongo進入shell命令模式,預設串連的資料庫是test資料庫,在此之前一定要確保你已經啟動了MongoDB,否則會出現錯誤,啟動之後運行成功,如:
[email protected]:/usr/src# mongo
MongoDB shell version: 2.4.9
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
> 
常用操作命令:
    db.help()                    help on db methods
    db.mycoll.help()             help on collection methods
    sh.help()                    sharding helpers
    rs.help()                    replica set helpers
    help admin                   administrative help
    help connect                 connecting to a db help
    help keys                    key shortcuts
    help misc                    misc things to know
    help mr                      mapreduce
    show dbs                     show database names
    show collections             show collections in current database
    show users                   show users in current database
    show profile                 show most recent system.profile entries with time >= 1ms
    show logs                    show the accessible logger names
    show log [name]              prints out the last segment of log in memory, ‘global‘ is default
    use <db_name>                set current database
    db.foo.find()                list objects in collection foo
    db.foo.find( { a : 1 } )     list objects in foo where a == 1
    it                           result of the last line evaluated; use to further iterate
    DBQuery.shellBatchSize = x   set default number of items to display on shell
    exit                         quit the mongo shell
 
Ubuntu下MongoDB的安裝和使用