日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python调用MongoDB使用心得

發(fā)布時間:2025/6/15 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python调用MongoDB使用心得 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文是一個Python?使用MongoDB的簡單教程,將使用pymongo對MongoDB進行的各種操作進行了簡單的匯總,NoSQLFan進行了簡單整理,使用Python的同學(xué)可以看一看。

下載相應(yīng)平臺的版本,解壓即可。為方便使用,將bin路徑添加到系統(tǒng)path環(huán)境變量里。其中mongod是服務(wù)器,mongo是客戶shell,然后創(chuàng)建數(shù)據(jù)文件目錄:在c盤下創(chuàng)建data文件夾,里面創(chuàng)建db文件夾。

基本使用:

安裝對應(yīng)語言的Driver,Python 安裝 pymongo

$ easy_install pymongo

使用方法總結(jié),摘自官方教程

創(chuàng)建連接

>>> import pymongo >>> connection=pymongo.Connection('localhost',27017)

切換數(shù)據(jù)庫

>>> db = connection.test_database

獲取collection

>>> collection = db.test_collection

db和collection都是延時創(chuàng)建的,在添加Document時才真正創(chuàng)建

文檔添加,_id自動創(chuàng)建

>>> import datetime >>> post = {"author": "Mike", ... "text": "My first blog post!", ... "tags": ["mongodb", "python", "pymongo"], ... "date": datetime.datetime.utcnow()} >>> posts = db.posts >>> posts.insert(post) ObjectId('...')

批量插入

>>> new_posts = [{"author": "Mike", ... "text": "Another post!", ... "tags": ["bulk", "insert"], ... "date": datetime.datetime(2009, 11, 12, 11, 14)}, ... {"author": "Eliot", ... "title": "MongoDB is fun", ... "text": "and pretty easy too!", ... "date": datetime.datetime(2009, 11, 10, 10, 45)}] >>> posts.insert(new_posts) [ObjectId('...'), ObjectId('...')]

獲取所有collection(相當(dāng)于SQL的show tables)

>>> db.collection_names() [u'posts', u'system.indexes']

獲取單個文檔

>>> posts.find_one() {u'date': datetime.datetime(...), u'text': u'My first blog post!', u'_id': ObjectId('...'), u'author': u'Mike', u'tags': [u'mongodb', u'python', u'pymongo']}

查詢多個文檔

>> for post in posts.find(): ... post ... {u'date': datetime.datetime(...), u'text': u'My first blog post!', u'_id': ObjectId('...'), u'author': u'Mike', u'tags': [u'mongodb', u'python', u'pymongo']} {u'date': datetime.datetime(2009, 11, 12, 11, 14), u'text': u'Another post!', u'_id': ObjectId('...'), u'author': u'Mike', u'tags': [u'bulk', u'insert']} {u'date': datetime.datetime(2009, 11, 10, 10, 45), u'text': u'and pretty easy too!', u'_id': ObjectId('...'), u'author': u'Eliot', u'title': u'MongoDB is fun'}

加條件的查詢

>>> posts.find_one({"author": "Mike"})

高級查詢

>>> posts.find({"date": {"$lt": d}}).sort("author")

統(tǒng)計數(shù)量

>>> posts.count() 3

加索引

>>> from pymongo import ASCENDING, DESCENDING >>> posts.create_index([("date", DESCENDING), ("author", ASCENDING)]) u'date_-1_author_1'

查看查詢語句的性能

>>> posts.find({"date": {"$lt": d}}).sort("author").explain()["cursor"] u'BtreeCursor date_-1_author_1' >>> posts.find({"date": {"$lt": d}}).sort("author").explain()["nscanned"] 2

附自己總結(jié)的一點小心得,僅供參考

缺點

  • 不是全盤取代傳統(tǒng)數(shù)據(jù)庫(NoSQLFan:是否能取代需要看應(yīng)用場景)
  • 不支持復(fù)雜事務(wù)(NoSQLFan:MongoDB只支持對單個文檔的原子操作)
  • 文檔中的整個樹,不易搜索,4MB限制?(NoSQLFan:1.8版本已經(jīng)修改為16M)

特點(NoSQLFan:作者在這里列舉的很多只是一些表層的特點):

  • 文檔型數(shù)據(jù)庫,表結(jié)構(gòu)可以內(nèi)嵌
  • 沒有模式,避免空字段開銷(Schema Free)
  • 分布式支持
  • 查詢支持正則
  • 動態(tài)擴展架構(gòu)
  • 32位的版本最多只能存儲2.5GB的數(shù)據(jù)(NoSQLFan:最大文件尺寸為2G,生產(chǎn)環(huán)境推薦64位)

名詞對應(yīng)

  • 一個數(shù)據(jù)項叫做 Document(NoSQLFan:對應(yīng)MySQL中的單條記錄)
  • 一個文檔嵌入另一個文檔(comment 嵌入 post)叫做 Embed
  • 儲存一系列文檔的地方叫做 Collections(NoSQLFan:對應(yīng)MySQL中的表)
  • 表間關(guān)聯(lián),叫做 Reference

總結(jié)

以上是生活随笔為你收集整理的Python调用MongoDB使用心得的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。