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创建临时文件夹的方法
Jul 06 Python
django 解决manage.py migrate无效的问题
May 27 Python
python2.x实现人民币转大写人民币
Jun 20 Python
10招!看骨灰级Pythoner玩转Python的方法
Apr 15 Python
为什么你还不懂得怎么使用Python协程
May 13 Python
python如何实现从视频中提取每秒图片
Oct 22 Python
对django后台admin下拉框进行过滤的实例
Jul 26 Python
通过实例解析Python调用json模块
Dec 11 Python
python时间与Unix时间戳相互转换方法详解
Feb 13 Python
使用Tensorboard工具查看Loss损失率
Feb 15 Python
最新2019Pycharm安装教程 亲测
Feb 28 Python
python中delattr删除对象方法的代码分析
Dec 15 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代码解析
2016/06/24 PHP
php使用file函数、fseek函数读取大文件效率对比分析
2016/11/04 PHP
浅谈PHP中类和对象的相关函数
2017/04/26 PHP
PHP 进度条函数的简单实例
2017/09/19 PHP
Yii框架日志记录Logging操作示例
2018/07/12 PHP
PHP5.6.8连接SQL Server 2008 R2数据库常用技巧分析总结
2019/05/06 PHP
在Laravel中实现使用AJAX动态刷新部分页面
2019/10/15 PHP
php libevent 功能与使用方法详解
2020/03/04 PHP
jquery 必填项判断表单是否为空的方法
2008/09/14 Javascript
了不起的node.js读书笔记之mongodb数据库交互
2014/12/22 Javascript
深入理解JavaScript的React框架的原理
2015/07/02 Javascript
AngularJS手动表单验证
2016/02/01 Javascript
浅谈jquery中的each方法$.each、this.each、$.fn.each
2016/06/23 Javascript
Angularjs结合Bootstrap制作的一个TODO List
2016/08/18 Javascript
gulp-uglify 与gulp.watch()配合使用时报错(重复压缩问题)
2016/08/24 Javascript
详解如何在Vue2中实现组件props双向绑定
2017/03/29 Javascript
页面点击小红心js实现代码
2018/05/26 Javascript
React中嵌套组件与被嵌套组件的通信过程
2018/07/11 Javascript
关于JavaScript 数组你应该知道的事情(推荐)
2019/04/10 Javascript
SSM+layUI 根据登录信息显示不同的页面方法
2019/09/20 Javascript
浅谈JavaScript节流和防抖函数
2020/08/25 Javascript
Vue实现小购物车功能
2020/12/21 Vue.js
python 实现红包随机生成算法的简单实例
2017/01/04 Python
python中pandas.DataFrame排除特定行方法示例
2017/03/12 Python
Python内置模块hashlib、hmac与uuid用法分析
2018/02/12 Python
python爬取足球直播吧五大联赛积分榜
2018/06/13 Python
python微信公众号之关键词自动回复
2018/06/15 Python
Python地图绘制实操详解
2019/03/04 Python
CSS3中的content属性使用示例
2015/07/20 HTML / CSS
英国领先的酒杯和水晶玻璃器皿制造商:Dartington Crystal
2019/06/23 全球购物
银行职员思想汇报
2013/12/31 职场文书
党员创先争优心得体会
2014/09/11 职场文书
天气温馨提示语
2015/07/14 职场文书
导游词之晋城蟒河
2019/12/12 职场文书
golang import自定义包方式
2021/04/29 Golang
python多线程方法详解
2022/01/18 Python