MONGODB 安裝

2014-04-26 21:52

1.下載 MongoDB

    下載官方網址 => https://www.mongodb.org/downloads  (如果是Windows安裝,請確認你的Windows版本併安裝搭配的版本)

2.建立資料庫儲存目錄

    例如 : C:\mongodb\data  預設會在 C:\data\db\

2.執行 MongoDB (Server)

    mongod.exe --dbpath C:\mongodb\data

3.測試連線

    步驟1 : http:/localhost:27017/ 應該會出現嘗試連接 MongoDB 27017 Port的訊息。

    步驟2 : c:\mongodb>mongo.exe 進行實際連線,如果成功應該會出現下面的類似訊息。

        MongoDB shell version: 2.4.5

        connecting to: test
 
4.切換所在資料庫
    指令 : use test1  (切換到 test1 這個資料庫)
 
5.新增資料
    指令1. : db.usercollection.insert({ "username" : "testuser1", "email" : "testuser1@testdomain.com" })
    回應 : WriteResult({ "nInserted" : 1 })   // 理論上會看起來像這樣。
    會在資料庫中增加一筆下面的紀錄。
{
    "_id" : ObjectId("5202b481d2184d390cbf6eca"),
    "username" : "testuser1",
    "email" : "testuser1@testdomain.com"
}
 
    指令2. : 
newstuff = [{ "username" : "testuser2", "email" : "testuser2@testdomain.com" }, { "username" : "testuser3", "email" : "testuser3@testdomain.com" }]
db.usercollection.insert(newstuff);
    回應 : 有點多懶得打了。
    會批次性增加下面幾筆記錄。
{
        "_id" : ObjectId("5202b481d2184d390cbf6eca"),
        "username" : "testuser1",
        "email" : "testuser1@testdomain.com"
}
{
        "_id" : ObjectId("5202b49ad2184d390cbf6ecb"),
        "username" : "testuser2",
        "email" : "testuser2@testdomain.com"
}
{
        "_id" : ObjectId("5202b49ad2184d390cbf6ecc"),
        "username" : "testuser3",
        "email" : "testuser3@testdomain.com"
}