MongoDB

認識mongodb

  • MongoDB 是一個介于關系數(shù)據(jù)庫和非關系數(shù)據(jù)庫之間的產(chǎn)品,是非關系數(shù)據(jù)庫當中功能最豐富,最像關系數(shù)據(jù)庫的。
  • MongoDB中將數(shù)據(jù)存儲為一個文檔,文檔由鍵值對(key=>value)組成,MongoDB 文檔類似于 JSON 對象。字段值可以包含其他文檔,數(shù)組及文檔數(shù)組。

MongoDB簡介

  • 基于分布式文件存儲的開源數(shù)據(jù)庫系統(tǒng)。
  • 旨在為WEB應用提供可擴展的高性能數(shù)據(jù)存儲解決方案。
  • 將數(shù)據(jù)存儲為一個文檔,文檔類似與Json格式。
{
    name:"小明",
    age:16,
    address: {city:"長沙", country:"china"}
}

MongoDB數(shù)據(jù)模型

  • 和關系型數(shù)據(jù)庫一樣,MongoDB存在數(shù)據(jù)庫的概念,一個MongoDB可以創(chuàng)建多個數(shù)據(jù)庫。
  • 多個鍵及其關聯(lián)的值有序地放置在一起就是文檔,文檔時MongoDB中數(shù)據(jù)的基本單元,是MongoDB的核心概念,很類似關系數(shù)據(jù)庫中的行(記錄)。
  • 集合就是一組文檔的組合,集合可以被看作關系型數(shù)據(jù)庫中的表。
RDBMS(關系型數(shù)據(jù)庫管理系統(tǒng)) MongoDB
Database(數(shù)據(jù)庫) Database(數(shù)據(jù)庫)
Table(表) Collection(集合)
Record(記錄) Document(文檔)
image.png

MongoDB進入與退出

mongo
exit

庫、集合操作

庫級操作語句

查看所有數(shù)據(jù)庫(空庫不會顯示)show dbs
image.png
切換/創(chuàng)建數(shù)據(jù)庫use 數(shù)據(jù)庫
image.png
  • use:有就切換,沒有就創(chuàng)建
查看當前所在庫db
image.png
刪除當前數(shù)據(jù)庫:db.dropDatabase()
image.png

集合操作

查看當前數(shù)據(jù)庫的集合 show collections
創(chuàng)建集合 (用引號引起來) db.createCollection(name, options)
刪除集合db.集合名稱.drop()
image.png

文檔操作

插入
db.集合名稱.insert(document)    
#插入文檔,    集合不存在會自動創(chuàng)建, 不能插入重復id的數(shù)
#_id 是12個字節(jié)十六進制數(shù)在一個集合的每個文檔是唯一的。如果不指定_id參數(shù),MongoDB會為文檔分配一個唯一的ObjectId

db.student.insert({name:'xiaobai'})
db.student.insert({name:'xiaobai', age:18})
db.student.insert({_id:1, name:'xiaobai', age:18})
#一次性插入多個
db.student.insert([
    {name:'juhao', sex:'男', age:18},
    {name:'nanbei', sex:'男', age:19},
    {name:'budong', sex:'男', age:20},
])
image.png
查詢
db.集合名稱.find()              #查詢所有
db.集合名稱.find().pretty()     #結構化顯示

db.集合.find({name:10}).pretty()          where name = 10
db.集合.find({name:{$ne:10} }).pretty()       where name != 10
db.集合.find({name:{$gt:10} }).pretty()       where name > 10
db.集合.find({name:{$lt:10} }).pretty()       where name < 10
#后面加個e就是加等于
    
#and邏輯
{$and:[{expression1}, {expression1}, ...]   }
#or邏輯
{$or:[{expression1}, {expression1}, ...]   }

#where sex='男' and age > 18
db.table.find({
    $and:[
        {sex:'男'}, {age:{$gt:18}}
    ]
})

#where sex='女' or age =18
db.table.find({
    $or:[
        {sex:'女'}, {age:18}
    ]
})
操作符 描述
$ne 不等于
$gt 大于
$lt 小于
$gte 大于等于
$lte 小于等于

更新

db.table.insert({
    name: 'juhao', 
    age: 18,
    height: 180,
    sex:'男',
    virtue: ['帥', '帥', '帥'],
})

db.table.update({sex:'男'},[{age:20},{height:180}])  
#更新第一條找到的文檔全部值  

db.table.update({sex:'男'}, {$set:{age:666, agee:6666}}) 
#修改第一條找到的文檔, key不存在就添加


db.table.update({sex:'男'}, {$set:{sex:'女'}}, {multi:true})  #更新滿足條件的全部數(shù)據(jù)

#前面查詢的條件也可以用在這里, wherer sex=男 and age>18
db.table.update(
    {$and:[{sex:'男'}, {age:{$gt:18}}]}, 
    {$set:{age:666}},
)

刪除

db.集合名稱.remove( <query>, <justOne> )
db.table.remove({age:18})       #刪除所有滿足條件的
db.table.remove({sex:'男'}, {justOne:true})  #刪除一條

#where sex='男' and age>18
db.table.remove({
    $and:[
        {sex:'男'}, {age:{$gt:18}}
    ]
})

Python與MogoDB交互

操作步驟

  • 下載模塊:
pip install pymongo
  • 建立連接:client = pymongo.MongoClient()
  • 指定數(shù)據(jù)庫:db = client[數(shù)據(jù)庫名]
  • 指定集合:collection=db[集合名]

基本使用

import pymongo


#建立連接:
client = pymongo.MongoClient()

#指定數(shù)據(jù)庫:
db = client['cainiao']

#指定集合:
collection = db['student']

#查找一條文檔:
result = collection.find_one()
print(result)

#查找所有:
all = collection.find()
print(all)
print(list(all))

#添加一條文檔:
add_result = collection.insert_one({'name':'xiaotan','age':18})
print(add_result)

#添加多條:
add_result_all = collection.insert_many([{'name': 'xiao1', 'age': 18}, {'name': 'xiao2', 'age': 18}])
print(add_result_all)

#刪除一條文檔:
del_result = collection.delete_one({'age': 18}) #刪除age=18的一條文檔
print(del_result)

#刪除多條:
del_result_all = collection.delete_many({'age': 18})#刪除age=18的全部文檔
print(del_result_all)

#修改一條文檔:
collection.update_one({'age': 19},{'$set':{'name':'cainiao110'} }) #將age=19的第一條記錄,修改name指,記住要用$set

#修改多條:update_many()
collection.update_many({'name':{'$ne':'xiaobai'}}, {'$set': {'age': 19}})

練習

  1. 將MongoDB的find, insert, update, remove方法封裝成類。
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容