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中Genarator函数用法分析
Apr 08 Python
python用Pygal如何生成漂亮的SVG图像详解
Feb 10 Python
Python面向对象class类属性及子类用法分析
Feb 02 Python
Python中常见的异常总结
Feb 20 Python
pytorch: tensor类型的构建与相互转换实例
Jul 26 Python
python 拼接文件路径的方法
Oct 23 Python
解决python Markdown模块乱码的问题
Feb 14 Python
Python pandas自定义函数的使用方法示例
Nov 20 Python
Python @property装饰器原理解析
Jan 22 Python
Selenium 滚动页面至元素可见的方法
Mar 18 Python
python3.6.5基于kerberos认证的hive和hdfs连接调用方式
Jun 06 Python
python如何提升爬虫效率
Sep 27 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
yii框架配置默认controller和action示例
2014/04/30 PHP
php获取网页中图片、DIV内容的简单方法
2014/06/19 PHP
laravel学习教程之关联模型
2016/07/30 PHP
PHP的PDO预定义常量讲解
2019/01/24 PHP
PHP生成短网址的思路以及实现方法的详解
2019/03/25 PHP
laravel 实现上传图片到本地和前台访问示例
2019/10/21 PHP
javascript 模拟点击广告
2010/01/02 Javascript
按钮接受回车事件的三种实现方法
2014/06/06 Javascript
javascript 继承学习心得总结
2016/03/17 Javascript
Canvas 制作动态进度加载水球详解及实例代码
2016/12/09 Javascript
微信小程序 两种滑动方式(横向滑动,竖向滑动)详细及实例代码
2017/01/13 Javascript
JavaScript实现的仿新浪微博原生态输入字数即时检查功能【兼容IE6】
2017/09/26 Javascript
vue.js整合mint-ui里的轮播图实例代码
2017/12/27 Javascript
详解js类型判断
2018/05/22 Javascript
vue 实现滚动到底部翻页效果(pc端)
2019/07/31 Javascript
Python实现获取域名所用服务器的真实IP
2015/10/25 Python
Python减少循环层次和缩进的技巧分析
2016/03/15 Python
Python 多线程的实例详解
2017/09/07 Python
python输入错误密码用户锁定实现方法
2017/11/27 Python
python3实现基于用户的协同过滤
2018/05/31 Python
对python 树状嵌套结构的实现思路详解
2019/08/09 Python
Django与pyecharts结合的实例代码
2020/05/13 Python
python开发一个解析protobuf文件的简单编译器
2020/11/17 Python
Gap加拿大官网:Gap Canada
2017/08/24 全球购物
鞋子女王塔玛拉·梅隆同名奢侈品牌:Tamara Mellon
2017/11/22 全球购物
英文版餐饮运营管理求职信
2013/11/06 职场文书
国际贸易专业个人求职信范文分享
2013/12/14 职场文书
教师岗位职责范本
2013/12/29 职场文书
公司担保书范文
2014/05/21 职场文书
公司董事长岗位职责
2014/06/08 职场文书
兽医医药专业求职信
2014/07/27 职场文书
党员个人自我评价
2015/03/03 职场文书
证婚人婚礼致辞
2015/07/28 职场文书
MySQL千万级数据表的优化实战记录
2021/08/04 MySQL
漫画「请问您今天要来点兔子吗?」最新杂志彩页公开
2022/03/24 日漫
Python中使用tkFileDialog实现文件选择、保存和路径选择
2022/05/20 Python