Python 使用pymongo操作mongodb库


版权声明:本文为博主原创文章,未经博主允许不得转载。
1,安装python3.5
如果Python还没有安装,可以直接用yum安装,
- # 不过安装的是2.6 version
- yum install -y python
源码安装3.5
- wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz
- tar -xvf Python-3.5.0.tgz
- cd Python-3.5.0
- ./configure --prefix=/usr/local--enable-shared
- make
- make install
- ln -s /usr/local/bin/python3 /usr/bin/python3
运行python之前需要配置库
echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf
ldconfig
运行演示
python3 --version
部分执行过程:
- [root@03_sdwm Python-3.5.0]# echo/usr/local/lib >> /etc/ld.so.conf.d/local.conf
- [root@03_sdwm Python-3.5.0]# ldconfig
- [root@03_sdwm Python-3.5.0]#
- [root@03_sdwm Python-3.5.0]#
- [root@03_sdwm Python-3.5.0]# python3--version
- Python 3.5.0
- [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
- [root@03_sdwm ~]# python3 -m easy_install pymongo
- Searching for pymongo
- Reading http://pypi.python.org/simple/pymongo/
- Best match: pymongo 3.4.0
- Downloading https://pypi.python.org/packages/82/26/f45f95841de5164c48e2e03aff7f0702e22cef2336238d212d8f93e91ea8/pymongo-3.4.0.tar.gz#md5=aa77f88e51e281c9f328cea701bb6f3e
- Processing pymongo-3.4.0.tar.gz
- Running pymongo-3.4.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-ZZv1Ig/pymongo-3.4.0/egg-dist-tmp-LRDmoy
- zip_safe flag not set; analyzing archive contents...
- Adding pymongo 3.4.0 to easy-install.pth file
- Installed /usr/lib/python2.6/site-packages/pymongo-3.4.0-py2.6-linux-x86_64.egg
- Processing dependencies for pymongo
- Finished processing dependencies for pymongo
- [root@03_sdwm ~]#
3,使用pymongo操作mongodb
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import pymongo
- import datetime
- def get_db():
- # 建立连接
- client = pymongo.MongoClient(host="10.244.25.180", port=27017)
- db = client['example']
- #或者 db = client.example
- return db
- def get_collection(db):
- # 选择集合(mongo中collection和database都是延时创建的)
- coll = db['informations']
- print db.collection_names()
- return coll
- def insert_one_doc(db):
- # 插入一个document
- coll = db['informations']
- information = {"name": "quyang", "age": "25"}
- information_id = coll.insert(information)
- print information_id
- def insert_multi_docs(db):
- # 批量插入documents,插入一个数组
- coll = db['informations']
- information = [{"name": "xiaoming", "age": "25"}, {"name": "xiaoqiang", "age": "24"}]
- information_id = coll.insert(information)
- print information_id
- def get_one_doc(db):
- # 有就返回一个,没有就返回None
- coll = db['informations']
- print coll.find_one() # 返回第一条记录
- print coll.find_one({"name": "quyang"})
- print coll.find_one({"name": "none"})
- def get_one_by_id(db):
- # 通过objectid来查找一个doc
- coll = db['informations']
- obj = coll.find_one()
- obj_id = obj["_id"]
- print "_id 为ObjectId类型,obj_id:" + str(obj_id)
- print coll.find_one({"_id": obj_id})
- # 需要注意这里的obj_id是一个对象,不是一个str,使用str类型作为_id的值无法找到记录
- print "_id 为str类型 "
- print coll.find_one({"_id": str(obj_id)})
- # 可以通过ObjectId方法把str转成ObjectId类型
- from bson.objectid import ObjectId
- print "_id 转换成ObjectId类型"
- print coll.find_one({"_id": ObjectId(str(obj_id))})
- def get_many_docs(db):
- # mongo中提供了过滤查找的方法,可以通过各种条件筛选来获取数据集,还可以对数据进行计数,排序等处理
- coll = db['informations']
- #ASCENDING = 1 升序;DESCENDING = -1降序;default is ASCENDING
- for item in coll.find().sort("age", pymongo.DESCENDING):
- print item
- count = coll.count()
- print "集合中所有数据 %s个" % int(count)
- #条件查询
- count = coll.find({"name":"quyang"}).count()
- print "quyang: %s"%count
- def clear_all_datas(db):
- #清空一个集合中的所有数据
- db["informations"].remove()
- if __name__ == '__main__':
- db = get_db()
- my_collection = get_collection(db)
- post = {"author": "Mike", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"],
- "date": datetime.datetime.utcnow()}
- # 插入记录
- my_collection.insert(post)
- insert_one_doc(db)
- # 条件查询
- print my_collection.find_one({"x": "10"})
- # 查询表中所有的数据
- for iii in my_collection.find():
- print iii
- print my_collection.count()
- my_collection.update({"author": "Mike"},
- {"author": "quyang", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"],
- "date": datetime.datetime.utcnow()})
- for jjj in my_collection.find():
- print jjj
- get_one_doc(db)
- get_one_by_id(db)
- get_many_docs(db)
- # clear_all_datas(db)
- mysql> show profile for query 4;
- +--------------------+----------+
- | Status | Duration |
- +--------------------+----------+
- | executing | 0.000017 |
- | Sending data | 0.018048 |
- | executing | 0.000028 |
- | Sending data | 0.018125 |
- | executing | 0.000022 |
- | Sending data | 0.015749 |
- | executing | 0.000017 |
- | Sending data | 0.015633 |
- | executing | 0.000017 |
- | Sending data | 0.015382 |
- | executing | 0.000015 |
- | Sending data | 0.015707 |
- | executing | 0.000023 |
- | Sending data | 0.015890 |
- | executing | 0.000022 |
- | Sending data | 0.015908 |
- | executing | 0.000017 |
- | Sending data | 0.015761 |
- | executing | 0.000022 |
- | Sending data | 0.015542 |
- | executing | 0.000014 |
- | Sending data | 0.015561 |
- | executing | 0.000016 |
- | Sending data | 0.015546 |
- | executing | 0.000037 |
- | Sending data | 0.015555 |
- | executing | 0.000015 |
- | Sending data | 0.015779 |
- | executing | 0.000026 |
- | Sending data | 0.015815 |
- | executing | 0.000015 |
- | Sending data | 0.015468 |
- | executing | 0.000015 |
- | Sending data | 0.015457 |
- | executing | 0.000015 |
- | Sending data | 0.015457 |
- | executing | 0.000014 |
- | Sending data | 0.015500 |
- | executing | 0.000014 |
- | Sending data | 0.015557 |
- | executing | 0.000015 |
- | Sending data | 0.015537 |
- | executing | 0.000014 |
- | Sending data | 0.015395 |
- | executing | 0.000021 |
- | Sending data | 0.015416 |
- | executing | 0.000014 |
- | Sending data | 0.015416 |
- | executing | 0.000014 |
- | Sending data | 0.015399 |
- | executing | 0.000023 |
- | Sending data | 0.015407 |
- | executing | 0.000014 |
- | Sending data | 0.015585 |
- | executing | 0.000014 |
- | Sending data | 0.015385 |
- | executing | 0.000014 |
- | Sending data | 0.015412 |
- | executing | 0.000014 |
- | Sending data | 0.015408 |
- | executing | 0.000014 |
- | Sending data | 0.015753 |
- | executing | 0.000014 |
- | Sending data | 0.015376 |
- | executing | 0.000014 |
- | Sending data | 0.015416 |
- | executing | 0.000019 |
- | Sending data | 0.015368 |
- | executing | 0.000014 |
- | Sending data | 0.015481 |
- | executing | 0.000015 |
- | Sending data | 0.015619 |
- | executing | 0.000015 |
- | Sending data | 0.015662 |
- | executing | 0.000016 |
- | Sending data | 0.015574 |
- | executing | 0.000015 |
- | Sending data | 0.015566 |
- | executing | 0.000015 |
- | Sending data | 0.015488 |
- | executing | 0.000013 |
- | Sending data | 0.015493 |
- | executing | 0.000015 |
- | Sending data | 0.015386 |
- | executing | 0.000015 |
- | Sending data | 0.015485 |
- | executing | 0.000018 |
- | Sending data | 0.015760 |
- | executing | 0.000014 |
- | Sending data | 0.015386 |
- | executing | 0.000015 |
- | Sending data | 0.015418 |
- | executing | 0.000014 |
- | Sending data | 0.015458 |
- | end | 0.000016 |
- | query end | 0.000019 |
- | closing tables | 0.000018 |
- | freeing items | 0.000825 |
- | logging slow query | 0.000067 |
- | cleaning up | 0.000025 |
- +--------------------+----------+
- 100 rows in set, 1 warning (0.00 sec)
- mysql>
Python 使用pymongo操作mongodb库的更多相关文章
- python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查
python操作mongodb数据库②python使用pymongo操作mongodb的增删改查 文档http://api.mongodb.com/python/current/api/index.h ...
- python 通过pymongo操作mongoDB执行sort
在mongo shell 中对数据进行排序操作的时候 db.getCollection('ANJUKE_PRICE').find({},{'id':1,'_id':0}).sort({'id':1}) ...
- MongoDB学习【四】—pymongo操作mongodb数据库
一.pymongodb的安装 Python 要连接 MongoDB 需要 MongoDB 驱动,这里我们使用 PyMongo 驱动来连接. pip安装 pip 是一个通用的 Python 包管理工具, ...
- python使用pymongo访问MongoDB的基本操作,以及CSV文件导出
1. 环境. Python:3.6.1 Python IDE:pycharm 系统:win7 2. 简单示例 import pymongo # mongodb服务的地址和端口号mongo_url = ...
- pymongo操作mongodb
此验证中只开启两个mongodb节点,可以连接任意节点,以下操作不涉及读写,不涉及连接那个节点 mongodb连接: from pymongo import MongoReplicaSetClient ...
- python(3):文件操作/os库
文件基本操作 r,以读模式打开, r+=r+w, w, 写模式(清空原来的内容), w+=w+r, a , 追加模式, a+=a+r, rb, wb, ab, b表示以二进制文件打开 想在一段文 ...
- Python操作MongoDB看这一篇就够了
MongoDB是由C++语言编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统,其内容存储形式类似JSON对象,它的字段值可以包含其他文档.数组及文档数组,非常灵活.在这一节中,我们就来看 ...
- python操作mongodb
# python操作mongodb # 首先,引入第三方模块pymongo,该模块是python用来操作mongodb的 import pymongo # 第二步,设置ip地址,以及表格名称,表格名字 ...
- 8.3 操作MongoDB数据库
一项权威调查显示,在大数据时代软件开发人员必备的十项技能中MongoDB数据库名列第二,仅次于HTML5.MongoDB是一个基于分布式文件存储的文档数据库,可以说是非关系型(Not Only SQL ...
随机推荐
- Java学习笔记八(反射)
1.介绍 反射为Java程序在执行时提供了动态的能力.利用反射能够在执行时对程序进行动态的控制.本篇博客着重解说一下Java中的反射. 2.Class类的使用 在Java执行过程中,每一个类被载入后都 ...
- 在触屏设备上面利用html5裁剪图片
前言 如今触屏设备越来越流行,并且大多数已经支持html5了.针对此.对触屏设备开发图片裁剪功能, 让其能够直接处理图片.减轻服务端压力. 技术点 浏览器必须支持html5,包含fileReader. ...
- 加快Qemu Aarch32虚拟开发板的启动速度
软件版本 Qemu: 2.8.0 虚拟开发板: vexpress-ca9 概述 之前的博文介绍了将Python移植到开发板上, 根文件系统采用的是ramdisk, 这个文件系统的缺点是修改的内容重启会 ...
- Android 实现页面跳转并传递参数教程
首先我们来看一下实现的功能: 第二,我们看一下实现这个功能,总共会接触到哪些文件和代码. 1.实现本功能总共涉及如下6个文件 2.实现本功能,总共涉及如下6个文件中的如下代码: (1) 效果: ...
- 从Oracle迁移到MySQL的各种坑及自救方案
当企业内部使用的数据库种类繁杂时,或者有需求更换数据库种类时,都可能会做很多数据迁移的工作.有些迁移很简单,有些迁移可能就会很复杂,大家有没有考虑过为了顺利完成复杂的数据库迁移任务,都需要考虑并解决哪 ...
- python测试开发django-20.添加创建时间DateTimeField
前言 我们在admin后台发布一篇文章的时候,一般会有创建时间和最后更新时间这2个字段,创建时间就是第一次编辑文章的时候自动添加的,最后更新时间就是每次修改文章的内容后自动更新 在models.py建 ...
- mysql中的日期转换函数(类似oracle中的to_date)
最近使用mysql开发一个项目,发现没有像之前在用oracle数据中那样有to_date方法.mysql中实现日期格式化的方法为date_to_str('日期字符串','日期格式'),而且mysql中 ...
- [A类会议] 国内论文检索
https://www.cn-ki.net/ http://www.koovin.com
- Mysql 编译安装并使用自定义用户启动
本文基于 Redhat Linux 6.7 的环境,Mysql 版本为 5.5.37 安装前的检查 必备的组件,如果没有使用 yum 进行安装,可以使用网上的源,也可以使用本地光盘作为 Yum 源. ...
- C# 程序设置开机启动(一)
一.示例使用VBScript创建启动文件,并添加到Windows系统的启动目录 默认情况下 所有用户启动目录:C:\ProgramData\Microsoft\Windows\Start Menu\P ...