Python操作MongoDb數(shù)據(jù)庫流程詳解
1.簡介
MongoDB是一個(gè)基于分布式文件存儲(chǔ)的文檔數(shù)據(jù)庫,可以說是非關(guān)系型(NoSQL,Not Only SQL)數(shù)據(jù)庫中比較像關(guān)系型數(shù)據(jù)庫的一個(gè),具有免費(fèi)、操作簡單、面向文檔、自動(dòng)分片、可擴(kuò)展性強(qiáng)、查詢功能強(qiáng)大等特點(diǎn),對(duì)大數(shù)據(jù)處理支持較好,旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。
MongoDB將數(shù)據(jù)存儲(chǔ)為一個(gè)文檔,數(shù)據(jù)結(jié)構(gòu)由鍵值(key=>value)對(duì)組成。MongoDB文檔類似于JSON對(duì)象。字段值可以包含其他文檔,數(shù)組及文檔數(shù)組。
2.應(yīng)用
MongoDB數(shù)據(jù)庫可以到網(wǎng)站https://www.mongodb.org/downloads下載,安裝之后打開命令提示符環(huán)境并切換到MongoDB安裝目錄中的server\3.2\bin文件夾,然后執(zhí)行命令mongod --dbpath D:\data --journal -- storageEngine=mmapv1啟動(dòng)MongoDB,當(dāng)然需要首先在D盤根目錄下新建文件夾data。
讓剛才那個(gè)命令提示符環(huán)境始終處于運(yùn)行狀態(tài),然后再打開一個(gè)命令提示符環(huán)境,執(zhí)行mongo命令連接MongoDB數(shù)據(jù)庫,如果連接成功的話,會(huì)顯示一個(gè)>符號(hào)作為提示符,之后就可以輸入MongoDB命令了。
打開或創(chuàng)建數(shù)據(jù)庫students
>use students
在數(shù)據(jù)庫中插入數(shù)據(jù)
>zhangsan = {‘name': ‘Zhangsan', ‘a(chǎn)ge': 18, ‘gender': ‘male'} >db.students.insert(zhangsan) >lisi = {‘name': ‘Lisi', ‘a(chǎn)ge': 19, ‘gender': ‘male'} >db.students.insert(lisi)
查詢數(shù)據(jù)庫中的記錄
>db.students.insert(lisi)
查詢數(shù)據(jù)庫中的記錄
>db.students.find()
查看系統(tǒng)中所有數(shù)據(jù)庫名稱
>show dbs
3.pymongo模塊
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : MaYi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2019-12-25 # Name : test01 # Software : PyCharm # Note : 應(yīng)用pymongo模塊操作MongoDB數(shù)據(jù)庫 # 導(dǎo)入模塊 import pymongo # 連接數(shù)據(jù)庫,27017是默認(rèn)端口 client = pymongo.MongoClient("localhost", 27017) # 獲取數(shù)據(jù)庫 db = client.students # 打印數(shù)據(jù)集合名稱列表 print(db.collection_names) # 獲取數(shù)據(jù)集合 res = db.students.find() print(res) for item in res: # 遍歷數(shù)據(jù) print(item) wangwu = {"name": "Wangwu", "age": 20, "sex": "male"} # 插入一條記錄 db.students.insert(wangwu) for item in db.students.find({"name": "Wangwu"}): # 指定查詢條件 print(item) # 獲取一條記錄 print(db.students.find_one()) print(db.students.find_one({"name": "Wangwu"})) # 記錄總數(shù) print(db.students.find().count()) # 刪除一條記錄 db.students.remove({"name": "Wangwu"}) # 創(chuàng)建索引 db.students.create_index([("name", pymongo.ASCENDING)]) # 更新數(shù)據(jù)庫 db.students.update({"name": "Zhangsan"}, {"$set": {"age": 25}}) # 清空數(shù)據(jù)庫 db.students.remove() # 插入多條數(shù)據(jù) zhangsan = {"name": "Zhangsan", "age": 20, "gender": "male"} lisi = {"name": "Lisi", "age": 21, "gender": "male"} wangwu = {"name": "Wangwu", "age": 22, "gender": "female"} db.students.insert_many([zhangsan, lisi, wangwu]) # 對(duì)查詢結(jié)果排序 for item in db.students.find().sort("name", pymongo.ASCENDING): print(item) # 入口函數(shù) if __name__ == '__main__': pass
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用Python操作MongoDB數(shù)據(jù)庫的詳細(xì)指南
- python?操作?mongodb?數(shù)據(jù)庫詳情
- MongoDB安裝使用并實(shí)現(xiàn)Python操作數(shù)據(jù)庫
- python連接、操作mongodb數(shù)據(jù)庫的方法實(shí)例詳解
- Python操作Mongodb數(shù)據(jù)庫的方法小結(jié)
- 淺析Python與Mongodb數(shù)據(jù)庫之間的操作方法
- Python使用pymongo庫操作MongoDB數(shù)據(jù)庫的方法實(shí)例
- 利用Python操作MongoDB數(shù)據(jù)庫的詳細(xì)指南
相關(guān)文章
python實(shí)現(xiàn)簡單的計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡單的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07pandas如何獲取某個(gè)數(shù)據(jù)的行號(hào)
這篇文章主要介紹了pandas如何獲取某個(gè)數(shù)據(jù)的行號(hào)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02PHP webshell檢查工具 python實(shí)現(xiàn)代碼
Web安全應(yīng)急響應(yīng)中,不免要檢查下服務(wù)器上是否被上傳了webshell,手工檢查比較慢,就寫了個(gè)腳本來檢查了。Windows平臺(tái)下已經(jīng)有了lake2寫的雷克圖的了,一般的檢查也夠用了,寫了個(gè)Linux下面的,用python寫的。2009-09-09python+requests接口壓力測(cè)試500次,查看響應(yīng)時(shí)間的實(shí)例
這篇文章主要介紹了python+requests接口壓力測(cè)試500次,查看響應(yīng)時(shí)間的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04Pytorch自定義Dataset和DataLoader去除不存在和空數(shù)據(jù)的操作
這篇文章主要介紹了Pytorch自定義Dataset和DataLoader去除不存在和空數(shù)據(jù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03簡單實(shí)現(xiàn)python進(jìn)度條腳本
這篇文章主要教大家如何簡單實(shí)現(xiàn)python進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12詳解如何通過Python實(shí)現(xiàn)批量數(shù)據(jù)提取
每天面對(duì)成堆的發(fā)票,無論是發(fā)票還是承兌單據(jù),抑或是其他各類公司數(shù)據(jù)要從照片、PDF等不同格式的內(nèi)容中提取,我們都有必要進(jìn)行快速辦公的能力提升。本文就教你如何利用Python實(shí)現(xiàn)批量數(shù)據(jù)提取吧2023-03-03python實(shí)現(xiàn)俄羅斯方塊游戲(改進(jìn)版)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)俄羅斯方塊游戲的改進(jìn)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03