python之PyMongo使用总结
作者:初心不忘 时间:2023-03-11 03:17:48
PyMongo是什么
PyMongo是驱动程序,使python程序能够使用Mongodb数据库,使用python编写而成.
安装
环境:Ubuntu 14.04+python2.7+MongoDB 2.4
先去官网下载软件包,地址点击打开链接.解压缩后进入,使用python setup.py install 进行安装
或者用pip安装pip -m install pymongo
基本使用
创建连接
import pymongo
client = pymongo.MongoClient('localhost', 27017)
或者可以这样
import pymongo
client = MongoClient('mongodb://localhost:27017/')
连接数据库
db = client.mydb
#或者
db = client['mydb']
连接聚集
聚集相当于关系型数据库中的表
collection = db.my_collection
#或者
collection = db['my_collection']
查看数据库下所有聚集名称
db.collection_names()
插入记录
collection.insert({"key1":"value1","key2","value2"})
删除记录
全部删除
collection.remove()
按条件删除
collection.remove({"key1":"value1"})
更新记录
collection.update({"key1": "value1"}, {"$set": {"key2": "value2", "key3": "value3"}})
查询记录
查询一条记录:find_one()不带任何参数返回第一条记录.带参数则按条件查找返回
collection.find_one()
collection.find_one({"key1":"value1"})
查询多条记录:find()不带参数返回所有记录,带参数按条件查找返回
collection.find()
collection.find({"key1":"value1"})
查看聚集的多条记录
for item in collection.find():
print item
查看聚集记录的总数
print collection.find().count()
查询结果排序
单列上排序
collection.find().sort("key1") # 默认为升序
collection.find().sort("key1", pymongo.ASCENDING) # 升序
collection.find().sort("key1", pymongo.DESCENDING) # 降序
多列上排序
collection.find().sort([("key1", pymongo.ASCENDING), ("key2", pymongo.DESCENDING)])
实例1:
#!/usr/bin/env python
#coding:utf-8
# Author: --<qingfengkuyu>
# Purpose: MongoDB的使用
# Created: 2014/4/14
#32位的版本最多只能存储2.5GB的数据(NoSQLFan:最大文件尺寸为2G,生产环境推荐64位)
import pymongo
import datetime
import random
#创建连接
conn = pymongo.Connection('10.11.1.70',27017)
#连接数据库
db = conn.study
#db = conn['study']
#打印所有聚集名称,连接聚集
print u'所有聚集:',db.collection_names()
posts = db.post
#posts = db['post']
print posts
#插入记录
new_post = {"AccountID":22,"UserName":"libing",'date':datetime.datetime.now()}
new_posts = [{"AccountID":22,"UserName":"liuw",'date':datetime.datetime.now()},
{"AccountID":23,"UserName":"urling",'date':datetime.datetime.now()}]#每条记录插入时间都不一样
posts.insert(new_post)
#posts.insert(new_posts)#批量插入多条数据
#删除记录
print u'删除指定记录:\n',posts.find_one({"AccountID":22,"UserName":"libing"})
posts.remove({"AccountID":22,"UserName":"libing"})
#修改聚集内的记录
posts.update({"UserName":"urling"},{"$set":{'AccountID':random.randint(20,50)}})
#查询记录,统计记录数量
print u'记录总计为:',posts.count(),posts.find().count()
print u'查询单条记录:\n',posts.find_one()
print posts.find_one({"UserName":"liuw"})
#查询所有记录
print u'查询多条记录:'
#for item in posts.find():#查询全部记录
#for item in posts.find({"UserName":"urling"}):#查询指定记录
#for item in posts.find().sort("UserName"):#查询结果根据UserName排序,默认为升序
#for item in posts.find().sort("UserName",pymongo.ASCENDING):#查询结果根据UserName排序,ASCENDING为升序,DESCENDING为降序
for item in posts.find().sort([("UserName",pymongo.ASCENDING),('date',pymongo.DESCENDING)]):#查询结果根据多列排序
print item
#查看查询语句的性能
#posts.create_index([("UserName", pymongo.ASCENDING), ("date", pymongo.DESCENDING)])#加索引
print posts.find().sort([("UserName",pymongo.ASCENDING),('date',pymongo.DESCENDING)]).explain()["cursor"]#未加索引用BasicCursor查询记录
print posts.find().sort([("UserName",pymongo.ASCENDING),('date',pymongo.DESCENDING)]).explain()["nscanned"]#查询语句执行时查询的记录数
来源:http://blog.csdn.net/shuaijiasanshao/article/details/51350881
标签:python,pymongo
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp下用OracleInProcServer完成对Oracle的连接和操作
2008-04-13 07:10:00
JavaScript的2008[译]
2009-02-20 13:49:00
![](https://img.aspxhome.com/file/UploadPic/up/8-javascript2008.gif)
Jmeter通过OS进程取样器调用Python脚本实现参数互传
2022-07-09 18:43:27
![](https://img.aspxhome.com/file/2023/8/68958_0s.png)
Kettle连接Oracle数据库方法((Oracle19c&Oracle11g))
2024-01-22 04:59:56
![](https://img.aspxhome.com/file/2023/7/131797_0s.png)
Python 循环语句之 while,for语句详解
2024-01-01 02:20:50
PHP加密函数 Javascript/Js 解密函数
2023-06-15 18:03:03
使用python解析json字段的3种方式实例
2021-12-24 04:16:27
![](https://img.aspxhome.com/file/2023/8/131318_0s.jpg)
django用户注册、登录、注销和用户扩展的示例
2021-09-09 13:11:05
![](https://img.aspxhome.com/file/2023/8/88888_0s.png)
Python+Selenium使用Page Object实现页面自动化测试
2022-09-30 06:09:57
Mysql锁内部实现机制之C源码解析
2024-01-12 21:13:25
![](https://img.aspxhome.com/file/2023/4/70454_0s.png)
你们要的Python绘画3D太阳系详细代码
2021-05-12 09:32:48
![](https://img.aspxhome.com/file/2023/1/86371_0s.gif)
Jupyter Notebook打开任意文件夹操作
2021-04-25 17:11:45
![](https://img.aspxhome.com/file/2023/8/92118_0s.jpg)
Python判断有效的数独算法示例
2021-10-09 06:37:37
Python打包方法Pyinstaller的使用
2022-06-08 13:25:21
![](https://img.aspxhome.com/file/2023/5/103645_0s.png)
python数据库编程 ODBC方式实现通讯录
2024-01-15 10:58:01
python实现贪吃蛇游戏源码
2021-07-22 12:45:24
JS实现简单的抽奖转盘效果示例
2024-04-22 22:29:39
![](https://img.aspxhome.com/file/2023/6/135706_0s.png)
数据库查询优化之子查询优化
2024-01-27 18:25:25
![](https://img.aspxhome.com/file/2023/4/97994_0s.png)
numpy存取数据(tofile/fromfile)的实现
2021-12-12 02:06:24
18个帮你简化代码的Python技巧分享
2021-08-12 02:41:12