2016-12-31 21:55 1115人阅读 评论(0) 收藏 举报
 分类:
- - - Python(10) 

版权声明:本文为博主原创文章,未经博主允许不得转载。

 

目录(?)[+]

 

1,安装python3.5

如果Python还没有安装,可以直接用yum安装,

  1. # 不过安装的是2.6 version
  2. yum install -y python

源码安装3.5

  1. wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz
  2. tar -xvf Python-3.5.0.tgz
  3. cd Python-3.5.0
  4. ./configure --prefix=/usr/local--enable-shared
  5. make
  6. make install
  7. ln -s /usr/local/bin/python3 /usr/bin/python3

运行python之前需要配置库

echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf

ldconfig

运行演示

python3 --version

部分执行过程:

  1. [root@03_sdwm Python-3.5.0]# echo/usr/local/lib >> /etc/ld.so.conf.d/local.conf
  2. [root@03_sdwm Python-3.5.0]# ldconfig
  3. [root@03_sdwm Python-3.5.0]#
  4. [root@03_sdwm Python-3.5.0]#
  5. [root@03_sdwm Python-3.5.0]# python3--version
  6. Python 3.5.0
  7. [root@03_sdwm Python-3.5.0]#

2,安装pymongo

安装方法有2种,分别是Installing with pip和Installing with easy_install,这里采用Installing witheasy_install参考官方文章:

http://api.mongodb.com/python/current/installation.html#installing-with-easy-install

安装python pymongo

  1. [root@03_sdwm ~]# python3 -m easy_install pymongo
  2. Searching for pymongo
  3. Reading http://pypi.python.org/simple/pymongo/
  4. Best match: pymongo 3.4.0
  5. Downloading https://pypi.python.org/packages/82/26/f45f95841de5164c48e2e03aff7f0702e22cef2336238d212d8f93e91ea8/pymongo-3.4.0.tar.gz#md5=aa77f88e51e281c9f328cea701bb6f3e
  6. Processing pymongo-3.4.0.tar.gz
  7. Running pymongo-3.4.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-ZZv1Ig/pymongo-3.4.0/egg-dist-tmp-LRDmoy
  8. zip_safe flag not set; analyzing archive contents...
  9. Adding pymongo 3.4.0 to easy-install.pth file
  10. Installed /usr/lib/python2.6/site-packages/pymongo-3.4.0-py2.6-linux-x86_64.egg
  11. Processing dependencies for pymongo
  12. Finished processing dependencies for pymongo
  13. [root@03_sdwm ~]#

3,使用pymongo操作mongodb

进行一些简单的对mongodb库的操作
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import pymongo
  4. import datetime
  5. def get_db():
  6. # 建立连接
  7. client = pymongo.MongoClient(host="10.244.25.180", port=27017)
  8. db = client['example']
  9. #或者 db = client.example
  10. return db
  11. def get_collection(db):
  12. # 选择集合(mongo中collection和database都是延时创建的)
  13. coll = db['informations']
  14. print db.collection_names()
  15. return coll
  16. def insert_one_doc(db):
  17. # 插入一个document
  18. coll = db['informations']
  19. information = {"name": "quyang", "age": "25"}
  20. information_id = coll.insert(information)
  21. print information_id
  22. def insert_multi_docs(db):
  23. # 批量插入documents,插入一个数组
  24. coll = db['informations']
  25. information = [{"name": "xiaoming", "age": "25"}, {"name": "xiaoqiang", "age": "24"}]
  26. information_id = coll.insert(information)
  27. print information_id
  28. def get_one_doc(db):
  29. # 有就返回一个,没有就返回None
  30. coll = db['informations']
  31. print coll.find_one()  # 返回第一条记录
  32. print coll.find_one({"name": "quyang"})
  33. print coll.find_one({"name": "none"})
  34. def get_one_by_id(db):
  35. # 通过objectid来查找一个doc
  36. coll = db['informations']
  37. obj = coll.find_one()
  38. obj_id = obj["_id"]
  39. print "_id 为ObjectId类型,obj_id:" + str(obj_id)
  40. print coll.find_one({"_id": obj_id})
  41. # 需要注意这里的obj_id是一个对象,不是一个str,使用str类型作为_id的值无法找到记录
  42. print "_id 为str类型 "
  43. print coll.find_one({"_id": str(obj_id)})
  44. # 可以通过ObjectId方法把str转成ObjectId类型
  45. from bson.objectid import ObjectId
  46. print "_id 转换成ObjectId类型"
  47. print coll.find_one({"_id": ObjectId(str(obj_id))})
  48. def get_many_docs(db):
  49. # mongo中提供了过滤查找的方法,可以通过各种条件筛选来获取数据集,还可以对数据进行计数,排序等处理
  50. coll = db['informations']
  51. #ASCENDING = 1 升序;DESCENDING = -1降序;default is ASCENDING
  52. for item in coll.find().sort("age", pymongo.DESCENDING):
  53. print item
  54. count = coll.count()
  55. print "集合中所有数据 %s个" % int(count)
  56. #条件查询
  57. count = coll.find({"name":"quyang"}).count()
  58. print "quyang: %s"%count
  59. def clear_all_datas(db):
  60. #清空一个集合中的所有数据
  61. db["informations"].remove()
  62. if __name__ == '__main__':
  63. db = get_db()
  64. my_collection = get_collection(db)
  65. post = {"author": "Mike", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"],
  66. "date": datetime.datetime.utcnow()}
  67. # 插入记录
  68. my_collection.insert(post)
  69. insert_one_doc(db)
  70. # 条件查询
  71. print my_collection.find_one({"x": "10"})
  72. # 查询表中所有的数据
  73. for iii in my_collection.find():
  74. print iii
  75. print my_collection.count()
  76. my_collection.update({"author": "Mike"},
  77. {"author": "quyang", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"],
  78. "date": datetime.datetime.utcnow()})
  79. for jjj in my_collection.find():
  80. print jjj
  81. get_one_doc(db)
  82. get_one_by_id(db)
  83. get_many_docs(db)
  84. # clear_all_datas(db)
  1. mysql> show profile for query 4;
  2. +--------------------+----------+
  3. | Status             | Duration |
  4. +--------------------+----------+
  5. | executing          | 0.000017 |
  6. | Sending data       | 0.018048 |
  7. | executing          | 0.000028 |
  8. | Sending data       | 0.018125 |
  9. | executing          | 0.000022 |
  10. | Sending data       | 0.015749 |
  11. | executing          | 0.000017 |
  12. | Sending data       | 0.015633 |
  13. | executing          | 0.000017 |
  14. | Sending data       | 0.015382 |
  15. | executing          | 0.000015 |
  16. | Sending data       | 0.015707 |
  17. | executing          | 0.000023 |
  18. | Sending data       | 0.015890 |
  19. | executing          | 0.000022 |
  20. | Sending data       | 0.015908 |
  21. | executing          | 0.000017 |
  22. | Sending data       | 0.015761 |
  23. | executing          | 0.000022 |
  24. | Sending data       | 0.015542 |
  25. | executing          | 0.000014 |
  26. | Sending data       | 0.015561 |
  27. | executing          | 0.000016 |
  28. | Sending data       | 0.015546 |
  29. | executing          | 0.000037 |
  30. | Sending data       | 0.015555 |
  31. | executing          | 0.000015 |
  32. | Sending data       | 0.015779 |
  33. | executing          | 0.000026 |
  34. | Sending data       | 0.015815 |
  35. | executing          | 0.000015 |
  36. | Sending data       | 0.015468 |
  37. | executing          | 0.000015 |
  38. | Sending data       | 0.015457 |
  39. | executing          | 0.000015 |
  40. | Sending data       | 0.015457 |
  41. | executing          | 0.000014 |
  42. | Sending data       | 0.015500 |
  43. | executing          | 0.000014 |
  44. | Sending data       | 0.015557 |
  45. | executing          | 0.000015 |
  46. | Sending data       | 0.015537 |
  47. | executing          | 0.000014 |
  48. | Sending data       | 0.015395 |
  49. | executing          | 0.000021 |
  50. | Sending data       | 0.015416 |
  51. | executing          | 0.000014 |
  52. | Sending data       | 0.015416 |
  53. | executing          | 0.000014 |
  54. | Sending data       | 0.015399 |
  55. | executing          | 0.000023 |
  56. | Sending data       | 0.015407 |
  57. | executing          | 0.000014 |
  58. | Sending data       | 0.015585 |
  59. | executing          | 0.000014 |
  60. | Sending data       | 0.015385 |
  61. | executing          | 0.000014 |
  62. | Sending data       | 0.015412 |
  63. | executing          | 0.000014 |
  64. | Sending data       | 0.015408 |
  65. | executing          | 0.000014 |
  66. | Sending data       | 0.015753 |
  67. | executing          | 0.000014 |
  68. | Sending data       | 0.015376 |
  69. | executing          | 0.000014 |
  70. | Sending data       | 0.015416 |
  71. | executing          | 0.000019 |
  72. | Sending data       | 0.015368 |
  73. | executing          | 0.000014 |
  74. | Sending data       | 0.015481 |
  75. | executing          | 0.000015 |
  76. | Sending data       | 0.015619 |
  77. | executing          | 0.000015 |
  78. | Sending data       | 0.015662 |
  79. | executing          | 0.000016 |
  80. | Sending data       | 0.015574 |
  81. | executing          | 0.000015 |
  82. | Sending data       | 0.015566 |
  83. | executing          | 0.000015 |
  84. | Sending data       | 0.015488 |
  85. | executing          | 0.000013 |
  86. | Sending data       | 0.015493 |
  87. | executing          | 0.000015 |
  88. | Sending data       | 0.015386 |
  89. | executing          | 0.000015 |
  90. | Sending data       | 0.015485 |
  91. | executing          | 0.000018 |
  92. | Sending data       | 0.015760 |
  93. | executing          | 0.000014 |
  94. | Sending data       | 0.015386 |
  95. | executing          | 0.000015 |
  96. | Sending data       | 0.015418 |
  97. | executing          | 0.000014 |
  98. | Sending data       | 0.015458 |
  99. | end                | 0.000016 |
  100. | query end          | 0.000019 |
  101. | closing tables     | 0.000018 |
  102. | freeing items      | 0.000825 |
  103. | logging slow query | 0.000067 |
  104. | cleaning up        | 0.000025 |
  105. +--------------------+----------+
  106. 100 rows in set, 1 warning (0.00 sec)
  107. mysql>

Python 使用pymongo操作mongodb库的更多相关文章

  1. python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查

    python操作mongodb数据库②python使用pymongo操作mongodb的增删改查 文档http://api.mongodb.com/python/current/api/index.h ...

  2. python 通过pymongo操作mongoDB执行sort

    在mongo shell 中对数据进行排序操作的时候 db.getCollection('ANJUKE_PRICE').find({},{'id':1,'_id':0}).sort({'id':1}) ...

  3. MongoDB学习【四】—pymongo操作mongodb数据库

    一.pymongodb的安装 Python 要连接 MongoDB 需要 MongoDB 驱动,这里我们使用 PyMongo 驱动来连接. pip安装 pip 是一个通用的 Python 包管理工具, ...

  4. python使用pymongo访问MongoDB的基本操作,以及CSV文件导出

    1. 环境. Python:3.6.1 Python IDE:pycharm 系统:win7 2. 简单示例 import pymongo # mongodb服务的地址和端口号mongo_url = ...

  5. pymongo操作mongodb

    此验证中只开启两个mongodb节点,可以连接任意节点,以下操作不涉及读写,不涉及连接那个节点 mongodb连接: from pymongo import MongoReplicaSetClient ...

  6. python(3):文件操作/os库

      文件基本操作 r,以读模式打开,  r+=r+w, w, 写模式(清空原来的内容), w+=w+r, a , 追加模式, a+=a+r, rb, wb, ab, b表示以二进制文件打开 想在一段文 ...

  7. Python操作MongoDB看这一篇就够了

    MongoDB是由C++语言编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统,其内容存储形式类似JSON对象,它的字段值可以包含其他文档.数组及文档数组,非常灵活.在这一节中,我们就来看 ...

  8. python操作mongodb

    # python操作mongodb # 首先,引入第三方模块pymongo,该模块是python用来操作mongodb的 import pymongo # 第二步,设置ip地址,以及表格名称,表格名字 ...

  9. 8.3 操作MongoDB数据库

    一项权威调查显示,在大数据时代软件开发人员必备的十项技能中MongoDB数据库名列第二,仅次于HTML5.MongoDB是一个基于分布式文件存储的文档数据库,可以说是非关系型(Not Only SQL ...

随机推荐

  1. 如何自定义 maven中的archetype

    1.首先使用eclipse创建一个新的maven project,然后把配置好的一些公用的东西放到相应的目录下面 比如说会将一些常用的java代码存放到src/main/java目录下面: 会将一些通 ...

  2. Android 数据存储03之SQLite

    SQLite数据存储 Android 集成了 SQLite 数据库.它存储在 /data/data/< 项目文件夹 >/databases/ 下.Android 开发中使用 SQLite ...

  3. java.lang.ThreadLocal类

    一.概述        ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并不是一个Thread,而是threadlocalvariable(线程局部变量).也 ...

  4. linux设置允许和禁止访问的IP host.allow 和 host.deny

    对于能过xinetd程序启动的网络服务,比如ftp telnet,我们就可以修改/etc/hosts.allow和/etc/hosts.deny的配制,来许可或者拒绝哪些IP.主机.用户可以访问. 比 ...

  5. 一分钟了解ArrayList和Vector的区别

    一.是否是线程安全的 Vector是同步的, 而ArrayList不是.因为Vector是同步的, 所以它是线程安全的.同样, 因为Vecotr是同步的, 所以他需要额外的开销来维持同步锁, 所以它要 ...

  6. 为何要对URL进行编码

    为何要对URL进行编码 我们都知道Http协议中参数的传输是"key=value"这种简直对形式的,如果要传多个参数就需要用“&”符号对键值对进行分割.如"?na ...

  7. Java命令学习系列(四)——jstat

    jstat(JVM Statistics Monitoring Tool)是用于监控虚拟机各种运行状态信息的命令行工具.他可以显示本地或远程虚拟机进程中的类装载.内存.垃圾收集.JIT编译等运行数据, ...

  8. C#程序集Assembly学习随笔(第一版)_AX

    ①什么是程序集?可以把程序集简单理解为你的.NET项目在编译后生成的*.exe或*.dll文件.嗯,这个确实简单了些,但我是这么理解的.详细:http://blog.csdn.net/sws8327/ ...

  9. Visual Studio Code 配置 gcc

    作者:谭九鼎链接:https://www.zhihu.com/question/30315894/answer/154979413来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  10. Netty端口被占用问题

    问题:   最近发现Netty项目每次发布的时候Netty在重启时都会报端口被占用的异常, 需要过十几秒左右手动重启一遍, Netty才能恢复正常 目前猜测是由于Tomcat_restart的时候Ne ...