MongoDB基本命令操作,MongoDB命令操作

來源:互聯網
上載者:User

MongoDB基本命令操作,MongoDB命令操作

在上一篇隨筆中記錄了如何在Centos7上安裝MongoDB資料庫,這一篇我們就一起來學學基本的操作命令。

安裝完成後,shell互動式下輸入mongo就可以直接無密碼登入到資料庫。

show dbs;                  #查看全部資料庫show collections;          #顯示當前資料庫中的集合(類似關聯式資料庫中的表)show users;                #查看當前資料庫的使用者資訊use <db name>;             #切換資料庫跟mysql一樣db;或者db.getName();        #查看當前所在資料庫db.help();                 #顯示資料庫操作命令,裡面有很多的命令 db.foo.help();             #顯示集合操作命令,同樣有很多的命令,foo指的是當前資料庫下,一個叫foo的集合,並非真正意義上的命令 db.foo.find();             #對於當前資料庫中的foo集合進行資料尋找(由於沒有條件,會列出所有資料) db.foo.find( { a : 1 } );  #對於當前資料庫中的foo集合進行尋找,條件是資料中有一個屬性叫a,且a的值為1

 

建立一個test資料庫例子:

> use test;             #建立資料庫switched to db test> db;               test> show dbs;           #檢查資料庫admin 0.000GBlocal 0.000GB> db.test.insert({"_id":"520","name":"xiaoming"})         #建立表WriteResult({ "nInserted" : 1 })> db.createUser({user:"xiaoming",pwd:"123456",roles:[{role:"userAdmin",db:"test"}]})        #建立使用者Successfully added user: {"user" : "xiaoming","roles" : [{"role" : "userAdmin","db" : "test"}]}
db.removeUser("userName"); #刪除使用者
show users; #顯示當前所有使用者

db.dropDatabase();   #刪除當前使用資料庫

> show dbs;admin 0.000GBlocal 0.000GBtest 0.000GBtest_1 0.000GB> db;test_1> db.dropDatabase();{ "dropped" : "test_1", "ok" : 1 }> show dbs;admin 0.000GBlocal 0.000GBtest 0.000GB

db.stats();             #顯示當前db狀態

> db.stats();{    "db" : "test_1",    "collections" : 0,    "views" : 0,    "objects" : 0,    "avgObjSize" : 0,    "dataSize" : 0,    "storageSize" : 0,    "numExtents" : 0,    "indexes" : 0,    "indexSize" : 0,    "fileSize" : 0,    "ok" : 1}

db.version();         #當前db版本

> db.version();3.4.10

db.getMongo();     #查看當前db的連結機器地址

> db.getMongo();connection to 172.16.40.205:27017

開啟遠端存取

編輯設定檔:vi /etc/mongod.confbindIp: 172.16.40.205      #資料庫所在伺服器IP地址儲存重啟資料庫!本地登入:mongo 172.16.40.205/admin -uadmin -p123456遠程登入:1. 下載mongodb壓縮包mongodb-linux-x86_64-3.4.10.tgz2. 解壓> tar zxvf mongodb-linux-x86_64-3.4.10.tgz3. 進入bin目錄> cd mongodb-linux-x86_64-3.4.10/bin4. 串連遠端資料庫> ./mongo 172.16.40.205:27017/admin  -u user  -p  password

 後續再總結髮布!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.