亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 5397 | 回復(fù): 0
打印 上一主題 下一主題

[MongoDB] python調(diào)用MongoDB使用心得 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2011-03-02 13:13 |只看該作者 |倒序?yàn)g覽
轉(zhuǎn):stonelee


python調(diào)用MongoDB使用心得






下載相應(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文件夾。

基本使用:



下載對(duì)應(yīng)語言的Driver
python

Python代碼
  1. $ easy_install pymongo
復(fù)制代碼
使用方法總結(jié),摘自官方教程

make a connection

Python代碼
  1. >>> import pymongo
  2. >>> connection=pymongo.Connection('localhost',27017)
復(fù)制代碼
get a database

Python代碼
  1. >>> db = connection.test_database
復(fù)制代碼
get a collection

Python代碼
  1. >>> collection = db.test_collection
復(fù)制代碼
db和collection都是延時(shí)創(chuàng)建的,在添加Document時(shí)才真正創(chuàng)建

Documents

Python代碼
  1. >>> import datetime
  2. >>> post = {"author": "Mike",
  3. ...         "text": "My first blog post!",
  4. ...         "tags": ["mongodb", "python", "pymongo"],
  5. ...         "date": datetime.datetime.utcnow()}
復(fù)制代碼
insert Document into collection
_id自動(dòng)創(chuàng)建

Python代碼
  1. >>> posts = db.posts
  2. >>> posts.insert(post)
  3. ObjectId('...')
復(fù)制代碼
批量插入

Python代碼
  1. >>> new_posts = [{"author": "Mike",
  2. ...               "text": "Another post!",
  3. ...               "tags": ["bulk", "insert"],
  4. ...               "date": datetime.datetime(2009, 11, 12, 11, 14)},
  5. ...              {"author": "Eliot",
  6. ...               "title": "MongoDB is fun",
  7. ...               "text": "and pretty easy too!",
  8. ...               "date": datetime.datetime(2009, 11, 10, 10, 45)}]
  9. >>> posts.insert(new_posts)
  10. [ObjectId('...'), ObjectId('...')]
復(fù)制代碼
list all collections

Python代碼
  1. >>> db.collection_names()
  2. [u'posts', u'system.indexes']
復(fù)制代碼
Get Single Document

Python代碼
  1. >>> posts.find_one()
  2. {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']}
復(fù)制代碼
查詢返回多個(gè)結(jié)果

Python代碼
  1. >> for post in posts.find():
  2. ...   post
  3. ...
  4. {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']}
  5. {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']}
  6. {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'}
復(fù)制代碼
加條件的查詢

Python代碼
  1. >>> posts.find_one({"author": "Mike"})
復(fù)制代碼
高級(jí)查詢

Python代碼
  1. >>> posts.find({"date": {"$lt": d}}).sort("author")
復(fù)制代碼
統(tǒng)計(jì)數(shù)量

Python代碼
  1. >>> posts.count()
  2. 3
復(fù)制代碼
加索引

Python代碼
  1. >>> from pymongo import ASCENDING, DESCENDING
  2. >>> posts.create_index([("date", DESCENDING), ("author", ASCENDING)])
  3. u'date_-1_author_1'
復(fù)制代碼
查看sql語句的性能

Python代碼
  1. >>> posts.find({"date": {"$lt": d}}).sort("author").explain()["cursor"]
  2. u'BtreeCursor date_-1_author_1'
  3. >>> posts.find({"date": {"$lt": d}}).sort("author").explain()["nscanned"]
  4. 2
復(fù)制代碼
附自己總結(jié)的一點(diǎn)小心得,僅供參考
not only sql

缺點(diǎn)
不是全盤取代傳統(tǒng)數(shù)據(jù)庫
不支持復(fù)雜事務(wù)
文檔中的整個(gè)樹,不易搜索,4MB限制?

特點(diǎn):
文檔型數(shù)據(jù)庫,表結(jié)構(gòu)可以內(nèi)嵌
沒有模式,避免空字段開銷
分布式支持
查詢支持正則
動(dòng)態(tài)擴(kuò)展架構(gòu)
32位的版本最多只能存儲(chǔ)2.5GB的數(shù)據(jù)

一個(gè)數(shù)據(jù)項(xiàng)叫做 Document
一個(gè)文檔嵌入另一個(gè)文檔(comment 嵌入 post)叫做 Embed
儲(chǔ)存一系列文檔的地方叫做 Collections
表間關(guān)聯(lián),叫做 Reference
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP