免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5356 | 回复: 0
打印 上一主题 下一主题

[MongoDB] python调用MongoDB使用心得 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-02 13:13 |只看该作者 |倒序浏览
转:stonelee


python调用MongoDB使用心得






下载相应平台的版本,解压即可。为方便使用,将bin路径添加到系统path环境变量里。其中mongod是服务器,mongo是客户shell
然后创建数据文件目录:在c盘下创建data文件夹,里面创建db文件夹。

基本使用:



下载对应语言的Driver
python

Python代码
  1. $ easy_install pymongo
复制代码
使用方法总结,摘自官方教程

make a connection

Python代码
  1. >>> import pymongo
  2. >>> connection=pymongo.Connection('localhost',27017)
复制代码
get a database

Python代码
  1. >>> db = connection.test_database
复制代码
get a collection

Python代码
  1. >>> collection = db.test_collection
复制代码
db和collection都是延时创建的,在添加Document时才真正创建

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()}
复制代码
insert Document into collection
_id自动创建

Python代码
  1. >>> posts = db.posts
  2. >>> posts.insert(post)
  3. ObjectId('...')
复制代码
批量插入

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('...')]
复制代码
list all collections

Python代码
  1. >>> db.collection_names()
  2. [u'posts', u'system.indexes']
复制代码
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']}
复制代码
查询返回多个结果

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'}
复制代码
加条件的查询

Python代码
  1. >>> posts.find_one({"author": "Mike"})
复制代码
高级查询

Python代码
  1. >>> posts.find({"date": {"$lt": d}}).sort("author")
复制代码
统计数量

Python代码
  1. >>> posts.count()
  2. 3
复制代码
加索引

Python代码
  1. >>> from pymongo import ASCENDING, DESCENDING
  2. >>> posts.create_index([("date", DESCENDING), ("author", ASCENDING)])
  3. u'date_-1_author_1'
复制代码
查看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
复制代码
附自己总结的一点小心得,仅供参考
not only sql

缺点
不是全盘取代传统数据库
不支持复杂事务
文档中的整个树,不易搜索,4MB限制?

特点:
文档型数据库,表结构可以内嵌
没有模式,避免空字段开销
分布式支持
查询支持正则
动态扩展架构
32位的版本最多只能存储2.5GB的数据

一个数据项叫做 Document
一个文档嵌入另一个文档(comment 嵌入 post)叫做 Embed
储存一系列文档的地方叫做 Collections
表间关联,叫做 Reference
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP