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实现linux下使用xcopy的方法
Jun 28 Python
Windows下python2.7.8安装图文教程
May 26 Python
5个很好的Python面试题问题答案及分析
Jan 19 Python
python读取和保存图片5种方法对比
Sep 12 Python
用Python实现大文本文件切割的方法
Jan 12 Python
Linux下通过python获取本机ip方法示例
Sep 06 Python
Python3安装pip工具的详细步骤
Oct 14 Python
python3.8 微信发送服务器监控报警消息代码实现
Nov 05 Python
pandas apply多线程实现代码
Aug 17 Python
Python+OpenCV图像处理——实现轮廓发现
Oct 23 Python
Numpy数组的广播机制的实现
Nov 03 Python
Python Serial串口基本操作(收发数据)
Nov 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
基于文本的搜索
2006/10/09 PHP
php程序效率优化的一些策略小结
2010/07/17 PHP
关于Zend Studio 配色方案插件的介绍
2013/06/24 PHP
解决php的“It is not safe to rely on the system’s timezone settings”问题
2015/10/08 PHP
PHP树-不需要递归的实现方法
2016/06/21 PHP
PHP实现的简单对称加密与解密方法实例小结
2017/08/28 PHP
php post json参数的传递和接收处理方法
2018/05/31 PHP
jQuery页面滚动浮动层智能定位实例代码
2011/08/23 Javascript
Javascript面向对象编程
2012/03/18 Javascript
Javascript图像处理—亮度对比度应用案例
2013/01/03 Javascript
javascript数组去重3种方法的性能测试与比较
2013/03/26 Javascript
js动画效果制件让图片组成动画代码分享
2014/01/14 Javascript
Javascript遍历table中的元素示例代码
2014/07/08 Javascript
jQuery表单美化插件jqTransform使用详解
2015/04/12 Javascript
在HTML中插入JavaScript代码的示例
2015/06/03 Javascript
浅谈Jquery核心函数
2015/06/18 Javascript
AngularJS模仿Form表单提交的实现代码
2016/12/08 Javascript
微信小程序实现简单的select下拉框
2020/11/23 Javascript
[10:54]Team Spirit vs Navi
2018/06/07 DOTA
Python pymongo模块用法示例
2018/03/31 Python
解决Python3中的中文字符编码的问题
2018/07/18 Python
pyqt5 禁止窗口最大化和禁止窗口拉伸的方法
2019/06/18 Python
django框架F&Q 聚合与分组操作示例
2019/12/12 Python
解决阿里云邮件发送不能使用25端口问题
2020/08/07 Python
matplotlib之多边形选区(PolygonSelector)的使用
2021/02/24 Python
CSS3中的clip-path使用攻略
2015/08/03 HTML / CSS
CSS3 linear-gradient线性渐变生成加号和减号的方法
2017/11/21 HTML / CSS
html5中canvas图表实现柱状图的示例
2017/11/13 HTML / CSS
适合各种场合的美食礼品:Harry & David
2016/08/03 全球购物
AJAX应用和传统Web应用有什么不同
2013/08/24 面试题
生产现场工艺工程师岗位职责
2013/11/28 职场文书
2014年元旦活动方案
2014/02/15 职场文书
小学数学新课改心得体会
2016/01/22 职场文书
导游词之宿迁乾隆行宫
2019/10/15 职场文书
HDFS免重启挂载新磁盘
2022/04/06 Servers
解决vue中provide inject的响应式监听
2022/04/19 Vue.js