相信大家都有聽過 NoSQL database, 而 Mongodb 就是 NoSQL database, 至於它的好處這邊就不佳贅述, 本篇文章主要是把我最近安裝MongoDb的步驟寫下來分享給各位參考
下載安裝Mangodb
設定環境
./mongod.exe --directoryperdb --dbpath "C:\Program Files\MongoDB\db" --logpath "C:\Program Files\MongoDB\log\mongo.log" --logappend --rest --install
--dbpath 告訴mogodb將資料儲存到db,
--logpath, --logappend 以append的方式將日誌儲存到 log\mongo.log,
--install 以service的方式啟動
--rest 提供 rest api 讓我們操作db
--logpath, --logappend 以append的方式將日誌儲存到 log\mongo.log,
--install 以service的方式啟動
--rest 提供 rest api 讓我們操作db
設定完成後會看到
2017-09-27T02:04:52.151-0700 I CONTROL [main] ** WARNING: --rest is specified without --httpinterface,
2017-09-27T02:04:52.207-0700 I CONTROL [main] ** enabling http interface
2017-09-27T02:04:52.207-0700 I CONTROL [main] ** enabling http interface
啟動資料庫
net start mongodb
The MongoDB service is starting.
The MongoDB service was started successfully.
The MongoDB service was started successfully.
設定使用者
首先我們必須先進入mongodb shell
C:\Program Files\MongoDB\Server\3.4\bin> .\mongo.exe
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
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
Server has startup warnings:
2017-09-27T02:05:15.560-0700 I CONTROL [main] ** WARNING: --rest is specified without --httpinterface,
2017-09-27T02:05:15.561-0700 I CONTROL [main] ** enabling http interface
2017-09-27T02:05:17.177-0700 I CONTROL [initandlisten]
2017-09-27T02:05:17.177-0700 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-09-27T02:05:17.177-0700 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
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
Server has startup warnings:
2017-09-27T02:05:15.560-0700 I CONTROL [main] ** WARNING: --rest is specified without --httpinterface,
2017-09-27T02:05:15.561-0700 I CONTROL [main] ** enabling http interface
2017-09-27T02:05:17.177-0700 I CONTROL [initandlisten]
2017-09-27T02:05:17.177-0700 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-09-27T02:05:17.177-0700 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
db.createUser({
user:"admin",
pwd:"1234",
roles: ["readWrite" , "dbAdmin"]
});
user:"admin",
pwd:"1234",
roles: ["readWrite" , "dbAdmin"]
});
建立 database
我們首先簡單地建立一個資料庫來放顧客資料
use mycustomers
show dbs
來看目前有那些資料庫在本機上建立 collection
這有點像是RDBMS 裡的 Tabl
db.createCollection('customers');
成功後會得到 { "ok" : 1 }
在有了 db, 管理者, collection 後, 接下來我們就可以開始對db做增刪修查
新增
db.customers.insert( { first_name: "andy" , last_name:"lai"} );
{ "_id" : ObjectId("59cb6f7f41267f4958c78183"), "first_name" : "andy", "last_name" : "lai" }
我們也可以一次新增多筆資料
db.customers.insert([{ first_name:"calvin", last_name:"chen"}, { first_name:"hugo", last_name:"lin", gender:"male"}] );
查詢
db.customers.find();
如果覺得顯示出來的資料太難看也可以在最後面加 .pretty()美化
修改
假設我想更新資料庫裡顧客資料 bob, 那其實我可使用底下的指令
db.customers.update({first_name:"bob"},{ first_name:"bob", last_name:"lin"});
其中, 第一個參數是查詢的條件, 第二個則是要更新的內容
若我們只是想修改其中一個欄位, 比如說姓氏 last_name 那我們可以這麼做
db.customers.update({first_name:"bob"},{ $set:{last_name:"wang"}});
刪除
如果我想刪除bob這個客戶, 則呼叫 remove()
db.customers.remove({first_name:"bob"});
如果是想刪除全部, 則可以傳一個空的JSON object 給 remove()
db.customers.remove({ });
留言
張貼留言