非關(guān)系型數(shù)據(jù)庫(kù)(NoSql)--MongoDB

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" }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、MongoDB簡(jiǎn)介 概述MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù),由C++語(yǔ)言編寫(xiě)。旨在為WEB應(yīng)用提供...
    未央_m閱讀 759評(píng)論 0 1
  • 一、MongoDB簡(jiǎn)介 概述MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù),由C++語(yǔ)言編寫(xiě)。旨在為WEB應(yīng)用提供...
    EndEvent閱讀 1,230評(píng)論 1 4
  • 一、MongoDB簡(jiǎn)介 1.概述 ? MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù),由C++語(yǔ)言編寫(xiě)。旨在為WE...
    鄭元吉閱讀 1,123評(píng)論 0 2
  • 一、MongoDB簡(jiǎn)介 概述MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù),由C++語(yǔ)言編寫(xiě)。旨在為WEB應(yīng)用提供...
    王梓懿_1fbc閱讀 564評(píng)論 0 3
  • 一、MongoDB簡(jiǎn)介 概述MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù),由C++語(yǔ)言編寫(xiě)。旨在為WEB應(yīng)用提供...
    fly5閱讀 358評(píng)論 0 0

友情鏈接更多精彩內(nèi)容