Python操作mongodb数据库进行模糊查询操作示例


Posted in Python onJune 09, 2018

本文实例讲述了Python操作mongodb数据库进行模糊查询操作。分享给大家供大家参考,具体如下:

# -*- coding: utf-8 -*-
import pymongo
import re
from pymongo import MongoClient
#创建连接
#10.20.66.106
client = MongoClient('10.20.4.79', 27017)
#client = MongoClient('10.20.66.106', 27017)
db_name = 'ta'
db = client[db_name]

假设mongodb数据库中school 集合中有一些数据记录

{ "_id" : 1, "zipcode" : "63109", "students" : { "comments" : "python abc" } }
{ "_id" : 2, "zipcode" : "63110", "students" : { "comments" : "python abc" } }
{ "_id" : 3, "zipcode" : "63109", "students" : { "comments" : "python abc" } }
{ "_id" : 4, "zipcode" : "63109", "students" : { "comments" : "python abc" } }
{ "_id" : 5, "zipcode" : "63109", "students" : { "comments" : "python abc" } }
{ "_id" : 7, "zipcode" : "63109", "students" : { "comments" : "python abc" }, "school" : "102 python abc" }
{ "_id" : 8, "zipcode" : "63109", "students" : { "comments" : "python abc" }, "school" : "100 python abc xyz" }
{ "_id" : 9, "zipcode" : "100", "students" : { "name" : "mike", "age" : 12, "comments" : "python" } }
{ "_id" : 10, "zipcode" : "100", "students" : { "name" : "Marry", "age" : 42, "comments" : "this is a python" } }
{ "_id" : 11, "zipcode" : "100", "students" : { "name" : "joe", "age" : 92, "comments" : "this is a python program" } }
{ "_id" : 12, "zipcode" : "100", "students" : { "name" : "joedd", "age" : 34, "comments" : "python is a script language" } }

现在要对students中comments的数据进行模糊查询, python中模糊查询要借助正则表达式:

1、查询comments中包含"abc"的记录:

for u in db.school.find({'students.comments':re.compile('abc')}):
print u

结果如下:

{u'students': {u'comments': u'python abc'}, u'_id': 1.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'_id': 2.0, u'zipcode': u'63110'}
{u'students': {u'comments': u'python abc'}, u'_id': 3.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'_id': 4.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'_id': 5.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'school': u'102 python abc', u'_id': 7.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'school': u'100 python abc xyz', u'_id': 8.0, u'zipcode': u'63109'}

2、查询comments中包含"this is"的记录:

for u in db.school.find({'students.comments':re.compile('this is')}):
print u

结果如下:

{u'students': {u'age': 42.0, u'name': u'Marry', u'comments': u'this is a python'}, u'_id': 10.0, u'zipcode': u'100'}
{u'students': {u'age': 92.0, u'name': u'joe', u'comments': u'this is a python program'}, u'_id': 11.0, u'zipcode': u'100'}

由此可见,模糊查询要用到re模块,查询条件利用re.compile()函数

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python+Wordpress制作小说站
Apr 14 Python
Python中用post、get方式提交数据的方法示例
Sep 22 Python
Python批量生成幻影坦克图片实例代码
Jun 04 Python
对Python的交互模式和直接运行.py文件的区别详解
Jun 29 Python
python opencv对图像进行旋转且不裁剪图片的实现方法
Jul 09 Python
Django实现web端tailf日志文件功能及实例详解
Jul 28 Python
Python实现生成密码字典的方法示例
Sep 02 Python
YUV转为jpg图像的实现
Dec 09 Python
Numpy一维线性插值函数的用法
Apr 22 Python
基于Tensorflow的MNIST手写数字识别分类
Jun 17 Python
Python图片检索之以图搜图
May 31 Python
Python 游戏大作炫酷机甲闯关游戏爆肝数千行代码实现案例进阶
Oct 16 Python
python 用lambda函数替换for循环的方法
Jun 09 #Python
python dataframe常见操作方法:实现取行、列、切片、统计特征值
Jun 09 #Python
python pandas 如何替换某列的一个值
Jun 09 #Python
pandas 对series和dataframe进行排序的实例
Jun 09 #Python
python pandas库中DataFrame对行和列的操作实例讲解
Jun 09 #Python
python pandas修改列属性的方法详解
Jun 09 #Python
numpy判断数值类型、过滤出数值型数据的方法
Jun 09 #Python
You might like
PHP HTML代码串 截取实现代码
2009/06/29 PHP
浅析echo(),print(),print_r(),return之间的区别
2013/11/27 PHP
PHP制作用户注册系统
2015/10/23 PHP
php的socket编程详解
2016/11/20 PHP
js+数组实现网页上显示时间/星期几的实用方法
2013/01/18 Javascript
Jqgrid表格随窗口大小改变而改变的简单实例
2013/12/28 Javascript
javascript数组去重方法汇总
2015/04/23 Javascript
JavaScript的Date()方法使用详解
2015/06/09 Javascript
跨域请求的完美解决方法(JSONP, CORS)
2016/06/12 Javascript
在javascript中使用com组件的简单实现方法
2016/08/17 Javascript
form表单序列化详解(推荐)
2017/08/15 Javascript
监听angularJs列表数据是否渲染完毕的方法示例
2018/11/07 Javascript
JavaScript实现的拼图算法分析
2019/02/13 Javascript
js动态获取时间的方法分析
2019/08/02 Javascript
微信小程序webview组件交互,内联h5页面并网页实现微信支付实现解析
2019/08/16 Javascript
nodejs环境使用Typeorm连接查询Oracle数据
2019/12/05 NodeJs
python 图片验证码代码
2008/12/07 Python
Python使用正则表达式获取网页中所需要的信息
2018/01/29 Python
pyqt5对用qt designer设计的窗体实现弹出子窗口的示例
2019/06/19 Python
python各层级目录下import方法代码实例
2020/01/20 Python
TensorFlow梯度求解tf.gradients实例
2020/02/04 Python
Python smtp邮件发送模块用法教程
2020/06/15 Python
Python面向对象实现方法总结
2020/08/12 Python
Python实现文件压缩和解压的示例代码
2020/08/12 Python
Python LMDB库的使用示例
2021/02/14 Python
HTML5中canvas中的beginPath()和closePath()的重要性
2018/08/24 HTML / CSS
SEPHORA丝芙兰捷克官网:购买香水、化妆品和护肤品
2018/11/26 全球购物
元旦联欢会策划方案
2014/06/11 职场文书
社会实践活动总结范文
2014/07/03 职场文书
纪检干部对照检查材料
2014/08/22 职场文书
教师节倡议书
2014/08/30 职场文书
检察院院长群众路线教育实践活动个人整改措施
2014/10/04 职场文书
大二学生自我检讨书
2014/10/23 职场文书
员工拾金不昧表扬稿
2015/05/05 职场文书
2016年暑期社会实践活动总结报告
2016/04/06 职场文书
Nginx反向代理至go-fastdfs案例讲解
2021/08/02 Servers