1. 程式人生 > >MongoDB安裝與基本語句

MongoDB安裝與基本語句

MongoDB是一種NoSql(非關係型資料庫)。

特點:(1)儲存資料量大:單表儲存可以達到PB級(1pb=1024T);(2)查詢速度快;(3)結構:資料庫==>集合==>文件;(4)適合儲存大量的資訊,丟失率相對較高。

Windows下安裝:1、可以從官網獲取最新的.msi安裝程式或者.zip的解壓版(http://dl.mongodb.org/dl/win32/x86_64)。(本人嘗試了.msi安裝,對於一些機器無法安裝成功;這裡就使用了zip解壓版。)

2、解壓版:將zip檔案解壓到D盤MongoDB檔案中。

一開始沒有data資料夾,自己新建一個,在data資料夾下新建一個db資料夾(存放資料庫檔案)和log資料夾,在log資料夾下新建一個空白的log.txt(存放日誌)。


3、開啟cmd,進入D:\MongoDB\bin,安裝MongoDB服務。(解除安裝:將install改為remove)

D:\MongoDB\bin>mongod.exe --dbpath=d:\mongodb\data\db\ --logpath=d:\mongodb\data\log\log.txt  --install

啟動服務

net start MongoDB

啟動mongodb

D:\MongoDB\bin>mongo.exe

正常會顯示以下資訊,在這裡會有個警告我們不關心。

MongoDB shell version v3.6.2-rc0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.2-rc0
Server has startup warnings:

到此安裝工作就成功了,接下來就嘗試一些基本命令。

操作:1、獲取當前資料庫名稱。

> db.getName()
test

2、建立資料庫

> use clare
switched to db clare
> db.getName()
clare

3、檢視資料庫狀態。

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

4、簡單的運算。

> x=100
100
> y=50
50
> x+y
150

5、檢視資料庫幫助資訊。

db.help()
DB methods:
        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs comma
nd [just calls db.runCommand(...)]
        db.aggregate([pipeline], {options}) - performs a collectionless aggregat
ion on this database; returns a cursor
        db.auth(username, password)
        db.cloneDatabase(fromhost)
        db.commandHelp(name) returns 
6、普通資料新增。
> db.user.insert({name:'clare',age:'23'})
WriteResult({ "nInserted" : 1 })
7、多維資料物件新增。
> db.user.insert({name:'Tung',age:'22',area:{province:'anhui',city:'anqing'}})
WriteResult({ "nInserted" : 1 })
8、陣列資訊新增。
> db.user.insert({name:'Tung1',age:'22',area:{province:'anhui',city:'anqing',hob
by:['film','run']}})
WriteResult({ "nInserted" : 1 })
9、查詢全部資料,_id欄位是MongoDB自動生成的,可以自己設定。
> db.user.find()
{ "_id" : ObjectId("5a5203c4aa29e75badb44396"), "name" : "clare", "age" : "23" }

{ "_id" : ObjectId("5a520483aa29e75badb44397"), "name" : "Tung", "age" : "22", "
area" : { "province" : "anhui", "city" : "anqing" } }
{ "_id" : ObjectId("5a520517aa29e75badb44398"), "name" : "Tung1", "age" : "22",
"area" : { "province" : "anhui", "city" : "anqing", "hobby" : [ "film", "run" ]
} }
10、查詢資料表第一條資料。
> db.user.findOne()
{
        "_id" : ObjectId("5a5203c4aa29e75badb44396"),
        "name" : "clare",
        "age" : "23"
}
11、帶條件的查詢,查詢名字為‘Tung’的資訊。
> db.user.find({name:'Tung'})
{ "_id" : ObjectId("5a520483aa29e75badb44397"), "name" : "Tung", "age" : "22", "
area" : { "province" : "anhui", "city" : "anqing" } }
12、findOne(條件):返回滿足條件的第一條資料。
> db.user.findOne({name:'Tung'})
{
        "_id" : ObjectId("5a520483aa29e75badb44397"),
        "name" : "Tung",
        "age" : "22",
        "area" : {
                "province" : "anhui",
                "city" : "anqing"
        }
}
13、範圍條件查詢:大於。
> db.user.find({monery:{'$gt':1000}})
{ "_id" : ObjectId("5a520722aa29e75badb4439a"), "name" : "t2", "monery" : 1500 }

{ "_id" : ObjectId("5a52075eaa29e75badb4439c"), "name" : "t4", "monery" : 1300 }
14、範圍條件查詢:大於等於。
> db.user.find({monery:{'$gte':1000}})
{ "_id" : ObjectId("5a520711aa29e75badb44399"), "name" : "t1", "monery" : 1000 }

{ "_id" : ObjectId("5a520722aa29e75badb4439a"), "name" : "t2", "monery" : 1500 }

{ "_id" : ObjectId("5a52075eaa29e75badb4439c"), "name" : "t4", "monery" : 1300 }

15、範圍條件查詢:小於。

> db.user.find({monery:{'$lt':1000}})
{ "_id" : ObjectId("5a52074eaa29e75badb4439b"), "name" : "t3", "monery" : 500 }

16、範圍條件查詢:小於等於。

> db.user.find({monery:{'$lte':1000}})
{ "_id" : ObjectId("5a520711aa29e75badb44399"), "name" : "t1", "monery" : 1000 }

{ "_id" : ObjectId("5a52074eaa29e75badb4439b"), "name" : "t3", "monery" : 500 }
17、多個條件查詢。
> db.user.find({name:'Tung',age:'22'})
{ "_id" : ObjectId("5a520483aa29e75badb44397"), "name" : "Tung", "age" : "22", "
area" : { "province" : "anhui", "city" : "anqing" } }
18、多維欄位查詢。
> db.user.find({'area.city':'anqing'})
{ "_id" : ObjectId("5a520483aa29e75badb44397"), "name" : "Tung", "age" : "22", "
area" : { "province" : "anhui", "city" : "anqing" } }
{ "_id" : ObjectId("5a520517aa29e75badb44398"), "name" : "Tung1", "age" : "22",
"area" : { "province" : "anhui", "city" : "anqing", "hobby" : [ "film", "run" ]
} }
19、陣列條件的限制。

①在陣列中就可以查詢到。

> db.goods.find({color:'red'})
{ "_id" : ObjectId("5a520c33aa29e75badb4439d"), "name" : "cup", "color" : [ "bla
nk", "red", "blue" ] }
②在陣列中存在一個就可以查詢到。
> db.goods.find({color:{'$all':['red','blue']}})
{ "_id" : ObjectId("5a520c33aa29e75badb4439d"), "name" : "cup", "color" : [ "bla
nk", "red", "blue" ] }
20、限制查詢欄位。

說明:要查詢的欄位設定為1,不需要查詢出來的欄位設定為0,但是1和0不能同時存在,_id除外。

> db.user.find({monery:{'$gt':1000}},{name:1})
{ "_id" : ObjectId("5a520722aa29e75badb4439a"), "name" : "t2" }
{ "_id" : ObjectId("5a52075eaa29e75badb4439c"), "name" : "t4" }
> db.user.find({monery:{'$gt':1000}},{name:0,monery:0})
{ "_id" : ObjectId("5a520722aa29e75badb4439a") }
{ "_id" : ObjectId("5a52075eaa29e75badb4439c") }
> db.user.find({monery:{'$gt':1000}},{name:1,monery:0})
Error: error: {
        "ok" : 0,
        "errmsg" : "Projection cannot have a mix of inclusion and exclusion.",
        "code" : 2,
        "codeName" : "BadValue"
}
21、$or多個條件查詢,滿足其中一個就行。
> db.user.find({'$or':[{name:'t2'},{monery:1000}]})
{ "_id" : ObjectId("5a520711aa29e75badb44399"), "name" : "t1", "monery" : 1000 }

{ "_id" : ObjectId("5a520722aa29e75badb4439a"), "name" : "t2", "monery" : 1500 }
22、修改資料。

①有$set的修改:只修改設定的欄位,其他欄位不變化。

> db.user.update({name:'Tung'},{'$set':{name:'d1'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
②沒有$set的修改:只修改設定的欄位,其他沒修改的欄位刪除了(_id)除外。
> db.user.update({name:'d1'},{name:'d2'})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
23、刪除資料。

①刪除記錄。

> db.user.remove({name:'clare'})
WriteResult({ "nRemoved" : 1 })
②刪除欄位。
> db.user.update({age:22},{'$unset':{age:0}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })