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写一个无界面的2048小游戏
May 24 Python
Python reduce()函数的用法小结
Nov 15 Python
Python使用pickle模块存储数据报错解决示例代码
Jan 26 Python
pandas 使用apply同时处理两列数据的方法
Apr 20 Python
Python批处理删除和重命名文件夹的实例
Jul 11 Python
Flask框架踩坑之ajax跨域请求实现
Feb 22 Python
Django框架文件上传与自定义图片上传路径、上传文件名操作分析
May 10 Python
python实现代码统计器
Sep 19 Python
简单了解python filter、map、reduce的区别
Jan 14 Python
python+flask编写一个简单的登录接口
Nov 13 Python
python爬虫selenium模块详解
Mar 30 Python
Python+OpenCV实现在图像上绘制矩形
Mar 21 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
星际争霸 Starcraft 游戏介绍
2020/03/14 星际争霸
php将textarea数据提交到mysql出现很多空格的解决方法
2014/12/19 PHP
php数组添加与删除单元的常用函数实例分析
2015/02/16 PHP
php生成图片缩略图的方法
2015/04/07 PHP
PHP获取文件夹大小函数用法实例
2015/07/01 PHP
适合PHP初学者阅读的4本经典书籍
2016/09/23 PHP
php session的应用详细介绍
2017/03/22 PHP
PHP 计算两个特别大的整数实例代码
2018/05/07 PHP
JS 文件传参及处理技巧分析
2010/05/13 Javascript
跨浏览器开发经验总结(四) 怎么写入剪贴板
2010/05/13 Javascript
深入理解javascript动态插入技术
2013/11/12 Javascript
JavaScript DOM操作表格及样式
2015/04/13 Javascript
nodejs导出excel的方法
2015/06/30 NodeJs
JS插件plupload.js实现多图上传并显示进度条
2016/11/29 Javascript
angularjs过滤器--filter与ng-repeat配合有奇效
2017/04/20 Javascript
浅析webpack 如何优雅的使用tree-shaking(摇树优化)
2017/08/16 Javascript
Angularjs实现多图片上传预览功能
2018/07/18 Javascript
微信小程序自定义导航栏(模板化)
2019/11/15 Javascript
详解element-ui 表单校验 Rules 配置 常用黑科技
2020/07/11 Javascript
python笔记(2)
2012/10/24 Python
使用Python装饰器在Django框架下去除冗余代码的教程
2015/04/16 Python
python利用datetime模块计算时间差
2015/08/04 Python
Python实现将数据框数据写入mongodb及mysql数据库的方法
2018/04/02 Python
Django数据库类库MySQLdb使用详解
2019/04/28 Python
python 统计list中各个元素出现的次数的几种方法
2021/02/20 Python
HTML5输入框下拉菜单功能的示例代码
2020/09/08 HTML / CSS
购买瑞典当代设计的腕表和太阳眼镜:TRIWA
2016/10/30 全球购物
Mio Skincare美国官网:身体紧致及孕期身体护理
2017/03/05 全球购物
Swisse官方海外旗舰店:澳大利亚销量领先,自然健康品牌
2017/12/15 全球购物
印刷工程专业应届生求职信
2013/09/29 职场文书
节能减耗标语
2014/06/21 职场文书
高中生旷课检讨书
2014/10/08 职场文书
离职报告范文
2014/11/04 职场文书
运动会表扬稿
2015/01/16 职场文书
关于销售人员的年终工作总结要点
2019/08/15 职场文书
解决Vue+SpringBoot+Shiro跨域问题
2021/06/09 Vue.js