温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python调用MongoDB的方法

发布时间:2021-03-09 14:16:29 来源:亿速云 阅读:252 作者:小新 栏目:数据库

这篇文章将为大家详细讲解有关Python调用MongoDB的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

  Python怎么调用MongoDB

  安装对应语言的Driver,Python安装pymongo

  $easy_installpymongo

  使用方法总结,摘自官方教程

  创建连接

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

  切换数据库

  >>>db=connection.test_database

  获取collection

  >>>collection=db.test_collection

  db和collection都是延时创建的,在添加Document时才真正创建

  文档添加,_id自动创建

  >>>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(相当于SQL的showtables)

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

  获取单个文档

  >>>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']}

  查询多个文档

  >>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"})

  高级查询

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

  统计数量

  >>>posts.count()
  3

  加索引

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

  查看查询语句的性能

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

  附自己总结的一点小心得,仅供参考

  缺点

  不是全盘取代传统数据库(NoSQLFan:是否能取代需要看应用场景)

  不支持复杂事务(NoSQLFan:MongoDB只支持对单个文档的原子操作)

  文档中的整个树,不易搜索,4MB限制?(NoSQLFan:1.8版本已经修改为16M)

  特点(NoSQLFan:作者在这里列举的很多只是一些表层的特点):

  文档型数据库,表结构可以内嵌

  没有模式,避免空字段开销(SchemaFree)

  分布式支持

  查询支持正则

  动态扩展架构

  32位的版本最多只能存储2.5GB的数据(NoSQLFan:最大文件尺寸为2G,生产环境推荐64位)

关于“Python调用MongoDB的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI