python调用MongoDB

1、安装python驱动pymongo

$easy_install pymongo

 

2、演示实例

  #!/usr/bin/python
  #-*-coding:utf-8-*-

  import pymongo
  import datetime

  #创建连接
  connection = pymongo.Connection('localhost', 27017)

  #切换数据库
  db = connection.blog_db

  #文档添加
  post = {"author" : "aho",
          "text" : "My first blog post!",
          "tags" : ["mongodb", "python", "pymongo"],
          "date" : datetime.datetime.utcnow()}
  posts = db.posts
  posts.insert(post)

  #查询单个文档
  re = posts.find_one()
  print re

  #查询多个文档
  for re in posts.find():
      print re

参考:http://blog.nosqlfan.com/html/2989.html

更多内容可参考官方文档:http://api.mongodb.org/python

 

posted on 2012-04-27 10:52  aho  阅读(1961)  评论(0编辑  收藏  举报

导航