MongoDB資料庫基本操作

來源:互聯網
上載者:User

標籤:

一、使用資料庫

顯示所有資料庫命令

$ ./mongoMongoDB shell version: 3.0.6connecting to: test> show dbslocal  0.078GBtest   0.078GB> 

顯示當前資料庫物件

$ ./mongoMongoDB shell version: 3.0.6connecting to: test> dbtest> 

切換資料庫

> use localswitched to db local> dblocal> 

 串連資料庫

從上面我們已經知道串連資料庫使用“mongo”命令,預設串連到test資料庫,當然這個命令還可以帶參數,如

$ ./mongo localhost:27017

表示串連到本機資料庫服務,localhost表示主機名稱,這裡也是預設串連到test資料庫

>dbtest>

注意後面的連接埠號碼必須加上否則,串連到localhost資料庫,即使localhost這個資料庫為空白

$ ./mongo localhostMongoDB shell version: 3.0.6connecting to: localhost> dblocalhost>show dbslocal  0.078Gtest   0.078G
>

 如果在其他程式設計語言中串連MongoDB服務時,需要提供連接字串,通用寫法為

mongodb://username:password@hostname/dbname
更多串連執行個體(此部分參考MongoDB菜鳥教程)

串連本機資料庫伺服器,連接埠是預設的。

mongodb://localhost

使用使用者名稱fred,密碼foobar登入localhost的admin資料庫。

mongodb://fred:foobar@localhost

使用使用者名稱fred,密碼foobar登入localhost的baz資料庫。

mongodb://fred:foobar@localhost/baz

串連 replica pair, 伺服器1為example1.com伺服器2為example2。

mongodb://example1.com:27017,example2.com:27017

串連 replica set 三台伺服器 (連接埠 27017, 27018, 和27019):

mongodb://localhost,localhost:27018,localhost:27019

串連 replica set 三台伺服器, 寫入操作應用在主伺服器 並且分布查詢到從伺服器。

mongodb://host1,host2,host3/?slaveOk=true

直接連接第一個伺服器,無論是replica set一部分或者主伺服器或者從伺服器。

mongodb://host1,host2,host3/?connect=direct;slaveOk=true

當你的串連伺服器有優先順序,還需要列出所有伺服器,你可以使用上述串連方式。

安全模式串連到localhost:

mongodb://localhost/?safe=true

以安全模式串連到replica set,並且等待至少兩個複製伺服器成功寫入,逾時時間設定為2秒。

mongodb://host1,host2,host3/?safe=true;w=2;wtimeoutMS=2000
參數選項說明

標準格式:

mongodb://[username:[email protected]]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

標準的串連格式包含了多個選項(options),如下所示:

 

選項 描述
replicaSet=name 驗證replica set的名稱。 Impliesconnect=replicaSet.
slaveOk=true|false
  • true:在connect=direct模式下,驅動會串連第一台機器,即使這台伺服器不是主。在connect=replicaSet模式下,驅動會發送所有的寫請求到主並且把讀取操作分布在其他從伺服器。
  • false: 在 connect=direct模式下,驅動會自動找尋主伺服器. 在connect=replicaSet 模式下,驅動僅僅串連主伺服器,並且所有的讀寫命令都串連到主伺服器。
safe=true|false
  • true: 在執行更新操作之後,驅動都會發送getLastError命令來確保更新成功。(還要參考 wtimeoutMS).
false: 在每次更新之後,驅動不會發送getLastError來確保更新成功。
w=n 驅動添加 { w : n } 到getLastError命令. 應用於safe=true。
wtimeoutMS=ms 驅動添加 { wtimeout : ms } 到 getlasterror 命令. 應用於 safe=true.
fsync=true|false
  • true: 驅動添加 { fsync : true } 到 getlasterror 命令.應用於 safe=true.
  • false: 驅動不會添加到getLastError命令中。
journal=true|false 如果設定為 true, 同步到 journal (在提交到資料庫前寫入到實體中). 應用於 safe=true
connectTimeoutMS=ms 可以開啟串連的時間。
socketTimeoutMS=ms 發送和接受sockets的時間。

 

二、建立資料庫

文法格式

use databasename

如果資料庫不存在,則建立資料庫,否則切換到指定資料庫。注意新建立的資料庫為空白,如不向其中插入記錄,則不會出現在show dbs語句的結果中

三、刪除資料庫

文法格式

db.dropDatabase()

刪除當前資料庫,預設為 test,你可以使用 db 命令查看當前資料庫名。

注意刪除了資料庫後,db所引用的資料庫仍然為被刪除的資料庫,只是這個資料庫此時為空白

MongoDB資料庫基本操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.