1. 程式人生 > >node.js學習筆記(6)--MongoDB簡單入門

node.js學習筆記(6)--MongoDB簡單入門

1.MongoDB bin目錄介紹

  mongo.exe:客戶端,支援js語法
  mongod.exe:服務端
  mongodump.exe:備份工具
  mongorestore.exe:恢復工具
  mongoexport.exe:匯出工具
  mongoimport.exe:匯入工具
  mongostat.exe:實時效能監控工具
  mongotop.exe:跟蹤MongDB例項讀寫時間工具

2.mongo.exe操作資料庫

上節我們已經把mongodb裝為windos服務了,所以可以簡單啟動
D:\MongoDB\Server\3.2\bin>net start mongodb
請求的服務已經啟動。

請鍵入 NET HELPMSG 2182 以獲得更多的幫助。


D:\MongoDB\Server\3.2\bin>
我的已經啟動了,我們開啟客戶端,可能和我的不一樣,不過沒什麼關係,看到 connecting to:test 說明連線成功。因為是支援js的,我們輸入1+1 會輸出結果 2
D:\MongoDB\Server\3.2\bin>mongo
2016-03-31T10:07:22.043+0800 I CONTROL  [main] Hotfix KB2731284 or later update
is not installed, will zero-out data files
MongoDB shell version: 3.2.4
connecting to: test
Server has startup warnings:
2016-03-31T09:50:33.443+0800 I CONTROL  [initandlisten]
2016-03-31T09:50:33.444+0800 I CONTROL  [initandlisten] ** WARNING: This 32-bit
MongoDB binary is deprecated
2016-03-31T09:50:33.444+0800 I CONTROL  [initandlisten]
2016-03-31T09:50:33.444+0800 I CONTROL  [initandlisten]
2016-03-31T09:50:33.444+0800 I CONTROL  [initandlisten] ** NOTE: This is a 32 bi
t MongoDB binary.
2016-03-31T09:50:33.444+0800 I CONTROL  [initandlisten] **       32 bit builds a
re limited to less than 2GB of data (or less with --journal).
2016-03-31T09:50:33.444+0800 I CONTROL  [initandlisten] **       Note that journ
aling defaults to off for 32 bit and is currently off.
2016-03-31T09:50:33.444+0800 I CONTROL  [initandlisten] **       See http://doch
ub.mongodb.org/core/32bit
2016-03-31T09:50:33.444+0800 I CONTROL  [initandlisten]
> 1+1
2
>

3.MongoDB基本語法

檢視資料庫:show dbs
> show dbs
local  0.078GB
test   0.078GB
>
新建資料庫:use  表名(新建資料庫必須進行此庫相關的操作,否則不會建立)
> use first
switched to db first
> show dbs
local  0.078GB
test   0.078GB
插入資料:db.表名.insert(資料) 
> db.first.insert({name:"tom",age:"12"})
WriteResult({ "nInserted" : 1 })
> show dbs
first  0.078GB
local  0.078GB
test   0.078GB
> 
查詢表中的所有資料:db.表名.find()
> db.first.find()
{ "_id" : ObjectId("56fc8e6d58257d7101dfafa0"), "name" : "tom", "age" : "12" }
>
修改資料:db.表名.update({"條件欄位名":"欄位值"},{$set:{"要修改的欄位名":"修改後的欄位值"}});
> db.first.update({name:"tom"},{$set: {age:13}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.first.find()
{ "_id" : ObjectId("56fc8e6d58257d7101dfafa0"), "name" : "tom", "age" : 13 }
>
批量插入資料:
> for(var i=1;i<10;i++){db.first.insert({name:"tom"+i,age:i}); }
WriteResult({ "nInserted" : 1 })
> db.first.find()
{ "_id" : ObjectId("56fc8e6d58257d7101dfafa0"), "name" : "tom", "age" : 13 }
{ "_id" : ObjectId("56fc907858257d7101dfafa1"), "name" : "tom1", "age" : 1 }
{ "_id" : ObjectId("56fc907858257d7101dfafa2"), "name" : "tom2", "age" : 2 }
{ "_id" : ObjectId("56fc907858257d7101dfafa3"), "name" : "tom3", "age" : 3 }
{ "_id" : ObjectId("56fc907858257d7101dfafa4"), "name" : "tom4", "age" : 4 }
{ "_id" : ObjectId("56fc907858257d7101dfafa5"), "name" : "tom5", "age" : 5 }
{ "_id" : ObjectId("56fc907858257d7101dfafa6"), "name" : "tom6", "age" : 6 }
{ "_id" : ObjectId("56fc907858257d7101dfafa7"), "name" : "tom7", "age" : 7 }
{ "_id" : ObjectId("56fc907858257d7101dfafa8"), "name" : "tom8", "age" : 8 }
{ "_id" : ObjectId("56fc907858257d7101dfafa9"), "name" : "tom9", "age" : 9 }
>

按條件查詢(支援多條件):db.表名.find(條件); 

> db.first.find({name:"tom1"})
{ "_id" : ObjectId("56fc907858257d7101dfafa1"), "name" : "tom1", "age" : 1 }
> db.first.find({"$or":[{name:"tom1"},{name:"tom2"}]})
{ "_id" : ObjectId("56fc907858257d7101dfafa1"), "name" : "tom1", "age" : 1 }
{ "_id" : ObjectId("56fc907858257d7101dfafa2"), "name" : "tom2", "age" : 2 }
>

刪除資料:db.表名.remove(條件)

> db.first.remove({name:"tom1"})
WriteResult({ "nRemoved" : 1 })
> db.first.find()
{ "_id" : ObjectId("56fc8e6d58257d7101dfafa0"), "name" : "tom", "age" : 13 }
{ "_id" : ObjectId("56fc907858257d7101dfafa2"), "name" : "tom2", "age" : 2 }
{ "_id" : ObjectId("56fc907858257d7101dfafa3"), "name" : "tom3", "age" : 3 }
{ "_id" : ObjectId("56fc907858257d7101dfafa4"), "name" : "tom4", "age" : 4 }
{ "_id" : ObjectId("56fc907858257d7101dfafa5"), "name" : "tom5", "age" : 5 }
{ "_id" : ObjectId("56fc907858257d7101dfafa6"), "name" : "tom6", "age" : 6 }
{ "_id" : ObjectId("56fc907858257d7101dfafa7"), "name" : "tom7", "age" : 7 }
{ "_id" : ObjectId("56fc907858257d7101dfafa8"), "name" : "tom8", "age" : 8 }
{ "_id" : ObjectId("56fc907858257d7101dfafa9"), "name" : "tom9", "age" : 9 }
>
檢視當前資料庫:db
切換資料庫:use 表名 刪除當前資料庫:db.dropDatabase()
> db
first
> use test
switched to db test
> db
test
> use first
switched to db first
> db.dropDatabase()
{ "dropped" : "first", "ok" : 1 }
> show dbs
local  0.078GB
test   0.078GB
>
介紹的應該能滿足基本操作,具體請看API

4.視覺化工具

是不是覺得控制檯寫程式碼太太太太難受了,所以我們需要一個視覺化工具 我用的是MongoBooster 安裝非常的簡單