溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python調(diào)用MongoDB的方法

發(fā)布時(shí)間:2021-03-09 14:16:29 來(lái)源:億速云 閱讀:262 作者:小新 欄目:數(shù)據(jù)庫(kù)

這篇文章將為大家詳細(xì)講解有關(guān)Python調(diào)用MongoDB的方法,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

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

  Python怎么調(diào)用MongoDB

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

  $easy_installpymongo

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

  創(chuàng)建連接

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

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

  >>>db=connection.test_database

  獲取collection

  >>>collection=db.test_collection

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

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

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

  批量插入

  >>>new_posts=[{"author":"Mike",
  ..."text":"Anotherpost!",
  ..."tags":["bulk","insert"],
  ..."date":datetime.datetime(2009,11,12,11,14)},
  ...{"author":"Eliot",
  ..."title":"MongoDBisfun",
  ..."text":"andprettyeasytoo!",
  ..."date":datetime.datetime(2009,11,10,10,45)}]
  >>>posts.insert(new_posts)
  [ObjectId('...'),ObjectId('...')]

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

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

  獲取單個(gè)文檔

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

  查詢多個(gè)文檔

  >>forpostinposts.find():
  ...post
  ...
  {u'date':datetime.datetime(...),u'text':u'Myfirstblogpost!',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'Anotherpost!',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'andprettyeasytoo!',u'_id':ObjectId('...'),u'author':u'Eliot',u'title':u'MongoDBisfun'}

  加條件的查詢

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

  高級(jí)查詢

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

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

  >>>posts.count()
  3

  加索引

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

  查看查詢語(yǔ)句的性能

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

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

  缺點(diǎn)

  不是全盤取代傳統(tǒng)數(shù)據(jù)庫(kù)(NoSQLFan:是否能取代需要看應(yīng)用場(chǎng)景)

  不支持復(fù)雜事務(wù)(NoSQLFan:MongoDB只支持對(duì)單個(gè)文檔的原子操作)

  文檔中的整個(gè)樹(shù),不易搜索,4MB限制?(NoSQLFan:1.8版本已經(jīng)修改為16M)

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

  文檔型數(shù)據(jù)庫(kù),表結(jié)構(gòu)可以內(nèi)嵌

  沒(méi)有模式,避免空字段開(kāi)銷(SchemaFree)

  分布式支持

  查詢支持正則

  動(dòng)態(tài)擴(kuò)展架構(gòu)

  32位的版本最多只能存儲(chǔ)2.5GB的數(shù)據(jù)(NoSQLFan:最大文件尺寸為2G,生產(chǎn)環(huán)境推薦64位)

關(guān)于“Python調(diào)用MongoDB的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI