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之做一个小游戏
Sep 28 Python
Python基于twisted实现简单的web服务器
Sep 29 Python
Python判断文本中消息重复次数的方法
Apr 27 Python
Python使用matplotlib简单绘图示例
Feb 01 Python
python获取代码运行时间的实例代码
Jun 11 Python
Python3.5基础之函数的定义与使用实例详解【参数、作用域、递归、重载等】
Apr 26 Python
基于Python+Appium实现京东双十一自动领金币功能
Oct 31 Python
利用keras加载训练好的.H5文件,并实现预测图片
Jan 24 Python
python Django 反向访问器的外键冲突解决
May 20 Python
没编程基础可以学python吗
Jun 17 Python
python使用Windows的wmic命令监控文件运行状况,如有异常发送邮件报警
Jan 30 Python
Django实现在线无水印抖音视频下载(附源码及地址)
May 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 URL跳转代码 减少外链
2011/06/25 PHP
改写ThinkPHP的U方法使其路由下分页正常
2014/07/02 PHP
javascript 进度条 实现代码
2009/07/30 Javascript
javascript 面向对象编程基础:封装
2009/08/21 Javascript
javascript 必知必会之closure
2009/09/21 Javascript
jquery中$.post()方法的简单实例
2014/02/04 Javascript
jquery实现效果比较好的table选中行颜色
2014/03/25 Javascript
javascript常见数据验证插件大全
2015/08/03 Javascript
浅析JavaScript函数的调用模式
2016/08/10 Javascript
详解js数组的完全随机排列算法
2016/12/16 Javascript
ajax的分页查询示例(不刷新页面)
2017/01/11 Javascript
Node.js与Sails redis组件的使用教程
2017/02/14 Javascript
js实现模糊匹配功能
2017/02/15 Javascript
vue-resource 拦截器使用详解
2017/02/21 Javascript
Angularjs的启动过程分析
2017/07/18 Javascript
微信小程序scroll-view组件实现滚动动画
2018/01/31 Javascript
解决layui调用自定义方法提示未定义的问题
2019/09/14 Javascript
vue实现购物车功能(商品分类)
2020/04/20 Javascript
python实现无证书加密解密实例
2014/10/27 Python
在Python下利用OpenCV来旋转图像的教程
2015/04/16 Python
Python 使用类写装饰器的小技巧
2018/09/30 Python
Python实现Linux监控的方法
2019/05/16 Python
Python列表与元组的异同详解
2019/07/02 Python
Numpy之将矩阵拉成向量的实例
2019/11/30 Python
浅谈Python 参数与变量
2020/06/20 Python
社区庆中秋节活动方案
2014/02/07 职场文书
服装促销活动方案
2014/02/23 职场文书
房产公证书范本
2014/04/10 职场文书
户外活动总结
2015/02/04 职场文书
公司门卫岗位职责
2015/04/13 职场文书
家庭教育培训学习心得体会
2016/01/14 职场文书
《悬崖边的树》读后感2篇
2019/12/02 职场文书
Nginx 502 Bad Gateway错误原因及解决方案
2021/03/31 Servers
python面向对象版学生信息管理系统
2021/06/24 Python
Java生成读取条形码和二维码的简单示例
2021/07/09 Java/Android
Java 使用类型为Object的变量指向任意类型的对象
2022/04/13 Java/Android