Robo 3Tmongodb
01數據庫
1.1 打開Robo官網ide
https://robomongo.org/學習
1.2 安裝Robo 3Tui
安裝Robo 3T 就可使用圖形界面,方便操做MongoDB數據庫,這個軟件安裝實在是太簡單了,我就放幾張圖喔,就幾張,哈哈哈!
spa
Robo 3T到此安裝完畢,實在是太簡單了。3d
Robo 3T 鏈接Win10的MongoDB,Linux也應該沒問題的,我就不演示了!目前小安我專一Python操做MongoDB就好了,之後有機會寫篇經過Python操做Linux的MongoDB。code
建立數據庫
orm
數據庫名字爲ipart(愛情公寓),最近在看這部電視劇,瘋狂刷1-5。blog
新建一張表,表名爲performer(演員?簡單點...說話的方式簡單點),忽然想唱歌。
Come some up music ...
MongoDB 快速入門
02
3.1 插入—Insert
爲何不是先講查詢?若是不先講插入數據,哪來數據供查詢呢!一切盡在個人掌握之中,嘿嘿!
3.1.1 InsertOne
顧名思義,插入一條數據。
db.getCollection('performer').insertOne({'name':'小安','sex':'man','age':19,'job':'不自由學習者','story':'愛情公寓的隔壁','best_wishes':'不清楚'})
3.1.2 InsertMany
顧名思義,一會兒插入N條數據。
db.getCollection('performer').insertMany([
{'name':'胡一菲','sex':'woman','age':23.5,'job':'彈一閃教師','story':'愛情公寓','best_wishes':'不清楚',},
{'name':'陳美嘉','sex':'woman','age':23,'job':'寵物店專員','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'唐悠悠','sex':'woman','age':22,'job':'專業演員','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'林宛瑜','sex':'woman','age':20,'job':'林氏才女','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'秦羽墨','sex':'woman','age':23,'job':'有錢人的追求者','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'諸葛大力','sex':'woman','age':19,'job':'最強大腦','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'咖喱醬','sex':'woman','age':19,'job':'吃貨','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'張大炮','sex':'man','age':25,'job':'二營長的大炮','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'呂子喬','sex':'man','age':26,'job':'鼓勵師','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'曾小賢','sex':'man','age':27,'job':'電臺主持人','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'關穀神奇','sex':'man','age':27,'job':'漫畫家','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'白開','sex':'man','age':100,'job':'小黑','best_wishes':'不清楚'},
{'name':'陸展博','sex':'man','age':21,'job':'外星人','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'張益達','sex':'man','age':29,'job':'倒黴的好人','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'斯內克','sex':'man','age':99,'story':'愛情公寓','best_wishes':'不清楚'},
{'name':'趙海棠','sex':'man','age':22,'job':'大文豪','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'張偉','sex':'man','age':26,'job':'律政先鋒','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'小則又沐風','sex':'woman','age':21,'job':'呂子喬的虛幻情人','story':'愛情公寓','best_wishes':'不清楚'},
{'name':'呂小布','sex':'man','age':25,'job':'僚機呂布'}
])
3.2 查詢—find
如下出現查詢數據的截圖,均來自每塊代碼的最後一條查詢語句。
3.2.1 查找數據
# 查找所有數據
# 相似關係型數據庫:" select * from performer "
db.getCollection('performer').find()
db.getCollection('performer').find({})
3.2.2 查詢指定條件的數據
# 知足一個條件
# 相似關係型數據庫:" select * from performer where age=20 "
db.getCollection('performer').find({'age':20)
# 同時知足兩個條件
# 相似關係型數據庫:" select * from performer where age=20 and sex='woman' "
db.getCollection('performer').find({'age':20,'sex':'woman'})
3.2.3 查詢指定範圍的數據
簡單說下限定範圍查詢的"暗號"
大於等於 : gte
少於等於 : lte
大於 : gt
少於 : lt
不等於 : ne
等於 : 你猜猜看
通常格式爲{列:值},若是在"值"裏面再套個{},那麼就能夠用於限定範圍。這真是懶出智慧啊,多簡單啊!
# 查詢年齡>=25的數據
# 相似關係型數據庫:" select * from performer where age>=25 "
db.getCollection('performer').find({'age':{'$gte':25}})
# 查詢21>年齡<25的數據
# 相似關係型數據庫:" select * from performer where age>21 and age<25 "
db.getCollection('performer').find({'age':{'$lt':25,'$gt':21}})
# 查詢21>年齡<25且名字不等於"小則又沐風"的數據
# 相似關係型數據庫:" select * from performer where age>21 and age<25 and name not in ('小則又沐風') "
db.getCollection('performer').find({'age':{'$lt':25,'$gt':21},'name':{'$ne','小則又沐風'}})
3.2.4 查詢限定列
({ })表明全部數據,若是在{ }旁邊加個逗號,不就是說明"橋多碼爹,還未結束",還有操做要進行。0代筆不顯示指定列,1表明顯示指定列。_id列默認是會顯示出來的,雖然_id很是重要,但MongoDB內部知道就行。爲了美觀,固然能夠經過 0 排除。
# 查詢數據但不返回列story,age
# 相似關係型數據庫:" select _id,name,sex,job,best_wishes from performer "
db.getCollection('performer').find({},{'story':0,'age':0})
# 查詢數據只返回列 name, age,但不返回object_id
# 相似關係型數據庫:" select name,age from performer "
db.getCollection('performer').find({},{'name':1,'age':1,'_id':0}).limit(5)
3.2.5 其餘
count要在查詢結果出來以後再.count,限定範圍也是要在查詢結果出來以後再.limit(行數),sort 列的值爲1,則正序;值爲-1,則倒序。
# 查詢年齡大於21歲的個數
# 相似關係型數據庫:" select count(*) from performer where age > 21 "
db.getCollection('performer').find({'age':{'$gt':21}}).count()
# 限定返回3條數據
# 相似關係型數據庫:" select * from performer where rownmum <=3 "
db.getCollection('performer').find({}).limit(3)
# 對查詢年齡少於24的結果進行倒序排序
# 相似關係型數據庫:" select * from performer where age < 21 order by age desc "
db.getCollection('performer').find({'age':{'$lt':24}}).sort({'age':-1})
3.3 更新—update
MongoDB的更新非爲updateOne和updateMany,目前還有新的功能,後續會在MongoDB高級用法的篇章說起,敬請期待。
3.3.1 updateOne
更新一條符合條件的數據,若是有多條數據符合,則從上往下開始更新,一次更新一條。
# 相似關係型數據庫:" update performer set age = 100 where name ='張益達' "
db.getCollection('performer').updateOne({'name':'張益達'},{'$set':{'age':100}})
3.3.2 updateMany
一次性更新全部符合條件的數據。
# 相似關係型數據庫:" update performer set story = '愛情公寓1-5',
best_wishes = '感謝陪伴,贊!' where story = '愛情公寓' "
db.getCollection('performer').updateMany({'story':'愛情公寓'},{'$set':{'story':'愛情公寓1-5','best_wishes':'感謝陪伴,贊!'}})
3.4 刪除—delete
MongoDB的更新非爲deleteOne和deleteMany。
3.4.1 deleteOne
刪除一條符合條件的數據,若是有多條數據符合,則從上往下開始刪除,一次刪除一條。
# 相似關係型數據庫:" delete from performer where name ='小安' "
db.getCollection('performer').deleteOne({'name':'小安'})
3.4.2 deleteMany
一次性刪除全部符合條件(sex='man')的數據。
# 相似關係型數據庫:" delete from performer where sex ='man' "
db.getCollection('performer').deleteMany({'sex':'man'})
Python鏈接MongoDB
03
照葫蘆畫瓢,IP加端口就能夠了。
1from pymongo import MongoClient
2client = MongoClient('mongodb://127.0.0.1:27017')
3print(client)
4# MongoClient(host=['127.0.0.1:27017'], document_class=dict, tz_aware=False, connect=True)
Python操做MongoDB
04