- 論壇徽章:
- 0
|
轉(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文件夾。
基本使用:
1.png (5.92 KB, 下載次數(shù): 39)
下載附件
2011-03-02 13:09 上傳
下載對(duì)應(yīng)語言的Driver
python
Python代碼使用方法總結(jié),摘自官方教程
make a connection
Python代碼- >>> import pymongo
- >>> connection=pymongo.Connection('localhost',27017)
復(fù)制代碼 get a database
Python代碼- >>> db = connection.test_database
復(fù)制代碼 get a collection
Python代碼- >>> collection = db.test_collection
復(fù)制代碼 db和collection都是延時(shí)創(chuàng)建的,在添加Document時(shí)才真正創(chuàng)建
Documents
Python代碼- >>> import datetime
- >>> post = {"author": "Mike",
- ... "text": "My first blog post!",
- ... "tags": ["mongodb", "python", "pymongo"],
- ... "date": datetime.datetime.utcnow()}
復(fù)制代碼 insert Document into collection
_id自動(dòng)創(chuàng)建
Python代碼- >>> posts = db.posts
- >>> posts.insert(post)
- ObjectId('...')
復(fù)制代碼 批量插入
Python代碼- >>> 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('...')]
復(fù)制代碼 list all collections
Python代碼- >>> db.collection_names()
- [u'posts', u'system.indexes']
復(fù)制代碼 Get Single Document
Python代碼- >>> 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']}
復(fù)制代碼 查詢返回多個(gè)結(jié)果
Python代碼- >> 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'}
復(fù)制代碼 加條件的查詢
Python代碼- >>> posts.find_one({"author": "Mike"})
復(fù)制代碼 高級(jí)查詢
Python代碼- >>> posts.find({"date": {"$lt": d}}).sort("author")
復(fù)制代碼 統(tǒng)計(jì)數(shù)量
Python代碼加索引
Python代碼- >>> from pymongo import ASCENDING, DESCENDING
- >>> posts.create_index([("date", DESCENDING), ("author", ASCENDING)])
- u'date_-1_author_1'
復(fù)制代碼 查看sql語句的性能
Python代碼- >>> 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
復(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 |
|