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的Django REST框架中的序列化及请求和返回
Apr 11 Python
Python3中简单的文件操作及两个简单小实例分享
Jun 18 Python
详解Python之unittest单元测试代码
Jan 24 Python
python实现下载pop3邮件保存到本地
Jun 19 Python
python调用百度语音识别实现大音频文件语音识别功能
Aug 30 Python
selenium+python自动化测试之鼠标和键盘事件
Jan 23 Python
pycharm创建一个python包方法图解
Apr 10 Python
使用python分析统计自己微信朋友的信息
Jul 19 Python
python自动结束mysql慢查询会话的实例代码
Oct 27 Python
python 实现矩阵按对角线打印
Nov 29 Python
python设置环境变量的作用整理
Feb 17 Python
Python爬虫防封ip的一些技巧
Aug 06 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读取富文本的时p标签会出现红线是怎么回事
2014/05/13 PHP
ThinkPHP的I方法使用详解
2014/06/18 PHP
PHP实现基本留言板功能原理与步骤详解
2020/03/26 PHP
JS 进度条效果实现代码整理
2011/05/21 Javascript
浅析Prototype的模板类 Template
2011/12/07 Javascript
jquery实现的鼠标下拉滚动置顶效果
2014/07/24 Javascript
使用百度地图api实现根据地址查询经纬度
2014/12/11 Javascript
JS实现Fisheye效果动感放大菜单代码
2015/10/21 Javascript
全面了解addEventListener和on的区别
2016/07/14 Javascript
node.js利用redis数据库缓存数据的方法
2017/03/01 Javascript
jquery.uploadView 实现图片预览上传功能
2017/08/10 jQuery
微信小程序使用slider设置数据值及switch开关组件功能【附源码下载】
2017/12/09 Javascript
vue中Axios的封装与API接口的管理详解
2018/08/09 Javascript
微信 jssdk 签名错误invalid signature的解决方法
2019/01/14 Javascript
JavaScript中的一些实用小技巧总结
2019/04/07 Javascript
Vue 页面权限控制和登陆验证功能的实例代码
2019/06/20 Javascript
JavaScript 继承 封装 多态实现及原理详解
2019/07/29 Javascript
vue.js循环radio的实例
2019/11/07 Javascript
[55:39]DOTA2-DPC中国联赛 正赛 VG vs LBZS BO3 第二场 1月19日
2021/03/11 DOTA
Python中的Matplotlib模块入门教程
2015/04/15 Python
在Python中处理时间之clock()方法的使用
2015/05/22 Python
基于python 字符编码的理解
2017/09/02 Python
对Python3.x版本print函数左右对齐详解
2018/12/22 Python
Python 正则表达式 re.match/re.search/re.sub的使用解析
2019/07/22 Python
python之pymysql模块简单应用示例代码
2019/12/16 Python
python实现opencv+scoket网络实时图传
2020/03/20 Python
python GUI模拟实现计算器
2020/06/22 Python
德国高性价比网上药店:medpex
2017/07/09 全球购物
应届生求职信范文
2014/05/26 职场文书
2014年旅游局法制宣传日活动总结
2014/11/01 职场文书
2014年宣传工作总结
2014/11/18 职场文书
辞职信范文大全
2015/03/02 职场文书
刑事上诉状范文
2015/05/22 职场文书
2016大学迎新晚会开场白
2015/11/24 职场文书
Mysql服务添加 iptables防火墙策略的方案
2021/04/29 MySQL
Python Pandas解析读写 CSV 文件
2022/04/11 Python