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获取系统默认字符编码的方法
Jun 04 Python
在类Unix系统上开始Python3编程入门
Aug 20 Python
Python全排列操作实例分析
Jul 24 Python
利用python循环创建多个文件的方法
Oct 25 Python
一篇文章弄懂Python中所有数组数据类型
Jun 23 Python
python处理excel绘制雷达图
Oct 18 Python
python修改文件内容的3种方法详解
Nov 15 Python
用python拟合等角螺线的实现示例
Dec 27 Python
TensorFlow内存管理bfc算法实例
Feb 03 Python
利用Python如何实时检测自身内存占用
May 09 Python
python 绘制正态曲线的示例
Sep 24 Python
pandas数据分组groupby()和统计函数agg()的使用
Mar 04 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
CodeIgniter php mvc框架 中国网站
2008/05/26 PHP
ThinkPHP3.1新特性之对Ajax的支持更加完善
2014/06/19 PHP
jquery常用技巧及常用方法列表集合
2011/04/06 Javascript
读jQuery之八 包装事件对象
2011/06/21 Javascript
兼容IE和Firefox火狐的上下、左右循环无间断滚动JS代码
2013/04/19 Javascript
Javascript实现滚动图片新闻的实例代码
2013/11/27 Javascript
js分页代码分享
2014/04/28 Javascript
jQuery获取checkboxlist的value值的方法
2015/09/27 Javascript
详解jQuery的Cookie插件
2016/11/23 Javascript
Vue.js学习之过滤器详解
2017/01/22 Javascript
Angular2中如何使用ngx-translate进行国际化
2017/05/21 Javascript
node.js中fs.stat与fs.fstat的区别详解
2017/06/01 Javascript
AngularJs实现聊天列表实时刷新功能
2017/06/15 Javascript
vue中appear的用法
2017/08/17 Javascript
基于vue1和vue2获取dom元素的方法
2018/03/17 Javascript
JS装饰器函数用法总结
2018/04/21 Javascript
vue使用技巧及vue项目中遇到的问题
2018/06/04 Javascript
使用layui的router来进行传参的实现方法
2019/09/06 Javascript
python解析发往本机的数据包示例 (解析数据包)
2014/01/16 Python
Windows和Linux下使用Python访问SqlServer的方法介绍
2015/03/10 Python
Python操作串口的方法
2015/06/17 Python
Python语法快速入门指南
2015/10/12 Python
tensorflow输出权重值和偏差的方法
2018/02/10 Python
python requests post多层字典的方法
2018/12/27 Python
在Python中使用Neo4j的方法
2019/03/14 Python
python re模块匹配贪婪和非贪婪模式详解
2020/02/11 Python
SQL里面如何插入自动增长序列号字段
2012/03/29 面试题
校园新闻广播稿
2014/01/10 职场文书
初二政治教学反思
2014/01/12 职场文书
学生会主席演讲稿
2014/04/25 职场文书
2014年向国旗敬礼活动总结
2014/09/27 职场文书
个人作风建设总结
2014/10/23 职场文书
创新创业项目计划书该怎样写?
2019/08/13 职场文书
带你学习MySQL执行计划
2021/05/31 MySQL
vue里使用create, mounted调用方法
2022/04/26 Vue.js
python 镜像环境搭建总结
2022/09/23 Python