MongoDB數(shù)據(jù)庫(kù)是除了redis之外的另外一個(gè)使用比較廣泛的非關(guān)系數(shù)據(jù)庫(kù),他是基于分布式文件存儲(chǔ)的開(kāi)源數(shù)據(jù)庫(kù)系統(tǒng),旨在為web應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案,將數(shù)據(jù)存儲(chǔ)為一個(gè)文檔,文檔類似與Json格式。
{
name:'zhangsan',
age:18,
address:{city:'beijing',county:'china'}
}
MongoDB數(shù)據(jù)類型比較:

MongoDB數(shù)據(jù)庫(kù)的操作:
一、MongoDB數(shù)據(jù)庫(kù)的進(jìn)入退出
C:\Users\Administrator>mongo #mongodb進(jìn)入
MongoDB shell version v4.0.9
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1852359e-6ed6-403b-95b9-1779c4cc4de6") }
MongoDB server version: 4.0.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:
2019-04-24T20:08:43.127-0700 I CONTROL [initandlisten]
2019-04-24T20:08:43.127-0700 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-04-24T20:08:43.127-0700 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-04-24T20:08:43.128-0700 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> exit #mongodb退出
bye
C:\Users\Administrator>mongo
MongoDB shell version v4.0.9
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8787bb6e-0845-44f0-8b01-7ace28b7ac8b") }
MongoDB server version: 4.0.9
Server has startup warnings:
2019-04-24T20:08:43.127-0700 I CONTROL [initandlisten]
2019-04-24T20:08:43.127-0700 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-04-24T20:08:43.127-0700 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-04-24T20:08:43.128-0700 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>
二、庫(kù),集合的操作
| 操作狀態(tài) | 操作指令 |
|---|---|
| 顯示所有庫(kù) | show dbs |
| 切換/創(chuàng)建數(shù)據(jù)庫(kù) | use 數(shù)據(jù)庫(kù)名稱 |
| 查看所在庫(kù) | db |
| 刪除庫(kù) | db.dropDatabase() |
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> use student
switched to db student
> db
student
> db.dropDatabase()
{ "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
集合操作:
| 操作狀態(tài) | 操作指令 |
|---|---|
| 顯示當(dāng)前數(shù)據(jù)庫(kù)集合 | show collections |
| 創(chuàng)建集合 | db.createCollection(name) |
| 刪除集合 | db.集合名稱.drop() |
> show collections
> db.createCollection('name')
{ "ok" : 1 }
> db.createCollection('age')
{ "ok" : 1 }
> show collections
age
name
> db.name.drop()
true
> db.age.drop()
true
>
三、文檔操作:
添加文檔數(shù)據(jù):
db.集合名稱.insert(document)
每一條數(shù)據(jù)就是一個(gè)文檔,就是一個(gè)json。添加數(shù)據(jù)時(shí)候,如果不指定_id參數(shù),MongoDB會(huì)自動(dòng)為文檔數(shù)據(jù)設(shè)置一個(gè)唯一的objectid
> db.stu.insert({'name':'zhangsan','age':18,'sex':'M','address':'beijing'})
WriteResult({ "nInserted" : 1 })
> db.stu.insert([
... {'name':'lisi','age':20,'sex':'F','address':'nanjing'},
... {'name':'wanger','age':21,'sex':'F','address':'beijing'},
... {'name':'mazi','age':19,'sex':'M','address':'shanghai'},
... ])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
查看文檔數(shù)據(jù):
db.集合名稱.find([condition])
MongoDB 的條件查詢語(yǔ)句非常強(qiáng)大,需要非常的細(xì)心,否則,容易出錯(cuò)。
| 操作狀態(tài) | 操作指令 |
|---|---|
| 查看集合中全部數(shù)據(jù) | db.集合名稱.find() |
| 格式化顯示 | db.集合名稱.find().pretty() |
| 條件查詢 | db.集合名稱.find({'address':'beijing'}) |
> db.stu.find()
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "name" : "zhangsan", "age" : 18, "sex" : "M", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855b"), "name" : "lisi", "age" : 20, "sex" : "F", "address" : "nanjing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "F", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855d"), "name" : "mazi", "age" : 19, "sex" : "M", "address" : "shanghai" }
> db.stu.find().pretty()
{
"_id" : ObjectId("5cc13a668eb8751da29b855a"),
"name" : "zhangsan",
"age" : 18,
"sex" : "M",
"address" : "beijing"
}
{
"_id" : ObjectId("5cc13ef48eb8751da29b855b"),
"name" : "lisi",
"age" : 20,
"sex" : "F",
"address" : "nanjing"
}
{
"_id" : ObjectId("5cc13ef48eb8751da29b855c"),
"name" : "wanger",
"age" : 21,
"sex" : "F",
"address" : "beijing"
}
{
"_id" : ObjectId("5cc13ef48eb8751da29b855d"),
"name" : "mazi",
"age" : 19,
"sex" : "M",
"address" : "shanghai"
}
> db.stu.find({'address':'beijing'})
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "name" : "zhangsan", "age" : 18, "sex" : "M", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "F", "address" : "beijing" }
查詢條件and,or,and or 混用:
在看and和or之前,我們先了解比較運(yùn)算的符號(hào):
| 操作符 | 描述 |
|---|---|
| $ne | 不等于 |
| $gt | 大于 |
| $lt | 小于 |
| $gte | 大于等于 |
| $lte | 小于等于 |
> db.stu.find({$and:[{'age':{$gte:18}},{'address':'beijing'}]})
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "name" : "zhangsan", "age" : 18, "sex" : "M", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "F", "address" : "beijing" }
>
修改文檔:
修改一條數(shù)據(jù):db.集合名稱.updata({條件},{修改值})
> db.stu.update({'name':'zhangsan'},{'age':30})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.stu.find()
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "age" : 30 }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855b"), "name" : "lisi", "age" : 20, "sex" : "F", "address" : "nanjing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "F", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855d"), "name" : "mazi", "age" : 19, "sex" : "M", "address" : "shanghai" }
>
可以看出,數(shù)據(jù)只剩下age:30
需要指定修改屬性{$set:{age:20}}
> db.stu.update({'name':'lisi'},{$set:{'age':35}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.stu.find()
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "age" : 30 }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855b"), "name" : "lisi", "age" : 35, "sex" : "F", "address" : "nanjing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "F", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855d"), "name" : "mazi", "age" : 19, "sex" : "M", "address" : "shanghai" }
更新集合中所有滿足條件的文檔{multi:true}
> db.stu.update({'sex':'F'},{$set:{'sex':'M'}},{multi:true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.stu.find()
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "age" : 30 }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855b"), "name" : "lisi", "age" : 35, "sex" : "M", "address" : "nanjing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "M", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855d"), "name" : "mazi", "age" : 19, "sex" : "M", "address" : "shanghai" }
刪除文檔:
| 操作狀態(tài) | 操作指令 |
|---|---|
| 刪除集合中所有的文檔 | db.table.remove({}) |
| 刪除集合中滿足條件的所有文檔 | db.table.remove({'sex':'F'}) |
| 只刪除集合中滿足條件的第一條文檔: { justOne: true } | db.table.remove({'sex':'F'}, { justOne: true }) |
> db.stu.remove({'name':'lisi'})
WriteResult({ "nRemoved" : 1 })
> db.stu.find()
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "age" : 30 }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "M", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855d"), "name" : "mazi", "age" : 19, "sex" : "M", "address" : "shanghai" }
> db.stu.remove({'sex':'M'},{justOne:true})
WriteResult({ "nRemoved" : 2 })
> db.stu.find()
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "age" : 30 }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855d"), "name" : "mazi", "age" : 19, "sex" : "M", "address" : "shanghai" }