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进阶教程之循环对象
Aug 30 Python
python的即时标记项目练习笔记
Sep 18 Python
Python中函数的多种格式和使用实例及小技巧
Apr 13 Python
Python实现根据IP地址和子网掩码算出网段的方法
Jul 30 Python
Python中在for循环中嵌套使用if和else语句的技巧
Jun 20 Python
Python无损音乐搜索引擎实现代码
Feb 02 Python
Python管理Windows服务小脚本
Mar 12 Python
python使用ddt过程中遇到的问题及解决方案【推荐】
Oct 29 Python
朴素贝叶斯Python实例及解析
Nov 19 Python
python GUI库图形界面开发之PyQt5选项卡控件QTabWidget详细使用方法与实例
Mar 01 Python
sklearn和keras的数据切分与交叉验证的实例详解
Jun 19 Python
Python 3.10 的首个 PEP 诞生,内置类型 zip() 迎来新特性(推荐)
Jul 03 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
Yii2 hasOne(), hasMany() 实现三表关联的方法(两种)
2017/02/15 PHP
Yii框架布局文件的动态切换操作示例
2019/11/11 PHP
php实现通过stomp协议连接ActiveMQ操作示例
2020/02/23 PHP
把textarea中字符串里含有的回车换行替换成<br>的javascript代码
2007/04/20 Javascript
JQuery 获取和设置Select选项的代码
2010/02/07 Javascript
JQuery 中几个类选择器的简单使用介绍
2013/03/14 Javascript
jquery关于事件冒泡和事件委托的技巧及阻止与允许事件冒泡的三种实现方法
2015/11/27 Javascript
JS组件Bootstrap Table表格行拖拽效果实现代码
2020/08/27 Javascript
如何用JavaScript实现动态修改CSS样式表
2016/05/20 Javascript
js+html5实现canvas绘制网页时钟的方法
2016/05/21 Javascript
微信小程序 canvas API详解及实例代码
2016/10/08 Javascript
原生js实现简单的Ripple按钮实例代码
2017/03/24 Javascript
js时间查询插件使用详解
2017/04/07 Javascript
vue中实现左右联动的效果
2018/06/22 Javascript
解决bootstrap-select 动态加载数据不显示的问题
2018/08/10 Javascript
在AngularJs中设置请求头信息(headers)的方法及不同方法的比较
2018/09/04 Javascript
vue服务端渲染添加缓存的方法
2018/09/18 Javascript
[04:40]DOTA2-DPC中国联赛1月26日Recap集锦
2021/03/11 DOTA
Python中用psycopg2模块操作PostgreSQL方法
2017/11/28 Python
关于Django显示时间你应该知道的一些问题
2017/12/25 Python
Django shell调试models输出的SQL语句方法
2019/08/29 Python
解决TensorFlow模型恢复报错的问题
2020/02/06 Python
python如何随机生成高强度密码
2020/08/19 Python
Python filter()及reduce()函数使用方法解析
2020/09/05 Python
波兰品牌内衣及泳装网上商店:Astratex.pl
2017/02/03 全球购物
迪拜航空官方网站:flydubai
2017/04/20 全球购物
Stefania Mode英国:奢华设计师和时尚服装
2017/10/23 全球购物
SHEIN台湾:购买最新流行女装服饰
2019/05/18 全球购物
全球烹饪课程的领先预订平台:Cookly
2020/01/28 全球购物
英国名牌男装店:Standout
2021/02/17 全球购物
什么是smarty? Smarty的优点是什么?
2013/08/11 面试题
毕业生的自我评价
2013/12/30 职场文书
工作的心得体会
2013/12/31 职场文书
学校读书活动总结
2014/06/30 职场文书
MySQL数据库如何给表设置约束详解
2022/03/13 MySQL
Python使用Web框架Flask开发项目
2022/06/01 Python