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实现ping的方法
Jul 06 Python
详解Python中的Cookie模块使用
Jul 06 Python
详解python上传文件和字符到PHP服务器
Nov 24 Python
Python数据分析之双色球统计单个红和蓝球哪个比例高的方法
Feb 03 Python
python绘制多个曲线的折线图
Mar 23 Python
Python实现数据结构线性链表(单链表)算法示例
May 04 Python
python数据预处理方式 :数据降维
Feb 24 Python
Python用来做Web开发的优势有哪些
Aug 05 Python
python Yaml、Json、Dict之间的转化
Oct 19 Python
最新pycharm安装教程
Nov 18 Python
Python偏函数实现原理及应用
Nov 20 Python
Python  lambda匿名函数和三元运算符
Apr 19 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
[FAQ]PHP中的一些常识:类篇
2006/10/09 PHP
php switch语句多个值匹配同一代码块的实现
2014/03/03 PHP
PHP中的多行字符串传递给JavaScript的两种方法
2014/06/19 PHP
PHP使用Face++接口开发微信公众平台人脸识别系统的方法
2015/04/17 PHP
PHP基于socket实现客户端和服务端通讯功能
2017/07/13 PHP
laravel-admin的图片删除实例
2019/09/30 PHP
Array的push与unshift方法性能比较分析
2011/03/05 Javascript
JavaScript 5 新增 Array 方法实现介绍
2012/02/06 Javascript
使用js修改客户端注册表的方法
2013/08/09 Javascript
JavaScript获取伪元素(Pseudo-Element)属性的方法技巧
2015/03/13 Javascript
JavaScript基本语法讲解
2015/06/03 Javascript
javascript中setInterval的用法
2015/07/19 Javascript
jQuery实现分隔条左右拖动功能
2015/11/21 Javascript
JS中的进制转换以及作用
2016/06/26 Javascript
JS中的hasOwnProperty()、propertyIsEnumerable()和isPrototypeOf()
2016/08/11 Javascript
原生js实现水平方向无缝滚动
2017/01/10 Javascript
微信小程序左右滑动的实现代码
2017/12/15 Javascript
vue路由前进后退动画效果的实现代码
2018/12/10 Javascript
vue 集成 vis-network 实现网络拓扑图的方法
2019/08/07 Javascript
Vue利用Blob下载原生二进制数组文件
2019/09/25 Javascript
微信小程序图片自适应实现解析
2020/01/21 Javascript
基于vue实现简易打地鼠游戏
2020/08/21 Javascript
[56:18]DOTA2上海特级锦标赛主赛事日 - 4 败者组第四轮#2 MVP.Phx VS Fnatic第二局
2016/03/05 DOTA
python和ruby,我选谁?
2017/09/13 Python
对python制作自己的数据集实例讲解
2018/12/12 Python
python感知机实现代码
2019/01/18 Python
django框架基于模板 生成 excel(xls) 文件操作示例
2019/06/19 Python
Python tkinter布局与按钮间距设置方式
2020/03/04 Python
python 实现简易的记事本
2020/11/30 Python
利用CSS3实现平移动画效果示例代码
2016/10/12 HTML / CSS
详解html5页面 rem 布局适配方法
2018/01/12 HTML / CSS
eBay意大利购物网站:eBay.it
2019/09/04 全球购物
成功的餐厅经营创业计划书
2014/01/15 职场文书
安全协议书范本
2014/04/21 职场文书
上班时间打瞌睡检讨书
2014/09/26 职场文书
班级元旦晚会开幕词
2016/03/04 职场文书