簡介:
有c++語言編寫,可為web應(yīng)用提供可擴(kuò)展的高性能能數(shù)據(jù)存儲记劈。
介于關(guān)系型數(shù)據(jù)庫與非關(guān)系型數(shù)據(jù)庫之間挪蹭,是非關(guān)系型數(shù)據(jù)庫當(dāng)中功能最豐富的。
支持的數(shù)據(jù)結(jié)構(gòu)松散咐扭,類似json的bson格式,即可存儲比較復(fù)雜的數(shù)據(jù)類型滑废。
特性:
特性 | |
---|---|
1 | 基于分布式文件存儲的開源數(shù)據(jù)系統(tǒng) |
2 | 可為web應(yīng)用提供可擴(kuò)展高性能的數(shù)據(jù)存儲方案 |
3 | 將數(shù)據(jù)存儲為一個文檔蝗肪,文檔類似與jason格式 |
MongoDB結(jié)構(gòu):
MongoDB | 關(guān)系型數(shù)據(jù)庫 |
---|---|
數(shù)據(jù)庫(database) | 數(shù)據(jù)庫(database) |
集合(collection) | 表(table) |
文檔(document) | 行(row) |
基本操作:
進(jìn)入mongodb數(shù)據(jù)庫:mongo
退出mongodb數(shù)據(jù)庫:exit
#進(jìn)入mongodb數(shù)據(jù)庫
Jayss@localhost:~$ mongo
MongoDB shell version: 2.6.10
connecting to: test
Server has startup warnings:
2019-02-03T23:43:03.529+0800 [initandlisten]
2019-02-03T23:43:03.529+0800 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
2019-02-03T23:43:03.529+0800 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
2019-02-03T23:43:03.529+0800 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
2019-02-03T23:43:03.529+0800 [initandlisten]
#退出mongodb數(shù)據(jù)庫
> exit
bye
庫級操作
#顯示所有數(shù)據(jù)庫
> show dbs
admin (empty)
local 0.078GB
python_foundation 0.078GB
test 0.078GB
#---------------------
#查看當(dāng)前所在庫
> db
test
#---------------------
#切換/創(chuàng)建數(shù)據(jù)庫:use 數(shù)據(jù)庫名 (當(dāng)數(shù)據(jù)庫名不存在時,則自動創(chuàng)建該數(shù)據(jù)庫)
> use local #切換local數(shù)據(jù)庫
switched to db local
> db #顯示當(dāng)前數(shù)據(jù)庫
local
> use study #創(chuàng)建study數(shù)據(jù)庫
switched to db study
> db
study
> show dbs #此時查看不到study數(shù)據(jù)庫的原因是study數(shù)據(jù)庫中無數(shù)據(jù)
admin (empty)
local 0.078GB
python_foundation 0.078GB
test 0.078GB
#---------------------
#刪除庫:db.dropDatabase()
> show dbs #顯示所有數(shù)據(jù)庫
admin (empty)
local 0.078GB
python_foundation 0.078GB
study 0.078GB
test 0.078GB
> use study #切換到study數(shù)據(jù)庫
switched to db study
> db.dropDatabase() #刪除當(dāng)前所在的數(shù)據(jù)庫
{ "dropped" : "study", "ok" : 1 }
> show dbs #顯示當(dāng)前所有的數(shù)據(jù)庫(可看到study數(shù)據(jù)庫已被刪除)
admin (empty)
local 0.078GB
python_foundation 0.078GB
test 0.078GB
集合操作
#查看當(dāng)前數(shù)據(jù)庫的集合:show collections
> use python_foundation #切換python_foundation數(shù)據(jù)庫
switched to db python_foundation
> db #顯示當(dāng)前所在數(shù)據(jù)庫
python_foundation
> show collections #顯示當(dāng)前所載數(shù)據(jù)庫的集合
student
system.indexes
#---------------------
#創(chuàng)建集合:db.createCollection(集合名)
> db.createCollection('user1') #創(chuàng)建名叫name的集合
{ "ok" : 1 }
> show collections #查看當(dāng)前數(shù)據(jù)庫所有集合
user1
system.indexes
#---------------------
#刪除集合
> show collections #查看當(dāng)前數(shù)據(jù)庫所有集合
name
student
system.indexes
> db.name.drop() #刪除名叫name的集合
true
> show collections #查看當(dāng)前數(shù)據(jù)庫所有集合
student
system.indexes
文檔操作
即添加文檔(數(shù)據(jù))
#查看集合中全部文檔:db.集合名.find()
> db.user1.find()
{ "_id" : ObjectId("5c62c1292ee6afdfb9c69779"), "name" : "jayss" }
#---------------------
#添加數(shù)據(jù):db.集合名.insert({數(shù)據(jù)})
> db.user1.insert({'name':'jayss'}) #為user1集合添加文檔
WriteResult({ "nInserted" : 1 })
> db.study.find() #查看user1集合的所有文檔
{ "_id" : ObjectId("5c62ad6a2ee6afdfb9c69775"), "name" : "jayss" }
#添加多條文檔
> db.user1.insert([
... {'age':20},
... {'sex':'male'}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
#查看集合user1的所有文檔
> db.user1.find()
{ "_id" : ObjectId("5c62c1292ee6afdfb9c69779"), "name" : "jayss" }
{ "_id" : ObjectId("5c62c2b977f12d5b7ee5fec9"), "age" : 20 }
{ "_id" : ObjectId("5c62c2b977f12d5b7ee5feca"), "sex" : "male" }
#---------------------
#修改一條數(shù)據(jù):db.集合名.update({舊數(shù)據(jù)},{$set:{新數(shù)據(jù)}})
> db.user1.find()
{ "_id" : ObjectId("5c62c1292ee6afdfb9c69779"), "name" : "jayss" }
{ "_id" : ObjectId("5c62c2b977f12d5b7ee5fec9"), "age" : 20 }
{ "_id" : ObjectId("5c62c2b977f12d5b7ee5feca"), "sex" : "male" }
> db.user1.update({'age':20},{$set:{'age':21}}) #把集合user1里的age等于20修改為21
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user1.find()
{ "_id" : ObjectId("5c62c1292ee6afdfb9c69779"), "name" : "jayss" }
{ "_id" : ObjectId("5c62c2b977f12d5b7ee5fec9"), "age" : 21 }
{ "_id" : ObjectId("5c62c2b977f12d5b7ee5feca"), "sex" : "male" }
#---------------------
#格式化顯示:db.集合名.find().pretty()
> db.user1.find().pretty()
{ "_id" : ObjectId("5c62ad6a2ee6afdfb9c69775"), "name" : "jayss" }
{ "_id" : ObjectId("5c62bc992ee6afdfb9c69776"), "age" : 20 }
{ "_id" : ObjectId("5c62bc992ee6afdfb9c69777"), "sex" : "male" }
#---------------------
#查看滿足條件的數(shù)據(jù):db.集合名.find({數(shù)據(jù)})
> db.user1.find({'name':'jayss'})
{ "_id" : ObjectId("5c62ad6a2ee6afdfb9c69775"), "name" : "jayss" }
#---------------------
#刪除集合中滿足條件的文檔
> db.user1.remove({'sex':'male'}) #刪除sex數(shù)據(jù)
WriteResult({ "nRemoved" : 1 })
> db.user1.find()
{ "_id" : ObjectId("5c62c1292ee6afdfb9c69779"), "name" : "jayss" }
{ "_id" : ObjectId("5c62c2b977f12d5b7ee5fec9"), "age" : 21 }
#---------------------
#刪除集合中所有文檔:db.集合名.remove()
> db.user1.remove()
歡迎技術(shù)交流
WeChat......