Python操作mongodb数据库进行模糊查询操作示例

 更新时间:2018年06月09日 12:47:33   作者:shaomine  
这篇文章主要介绍了Python操作mongodb数据库进行模糊查询操作,结合实例形式分析了Python连接MongoDB数据库及使用正则表达式进行模糊查询的相关操作技巧,需要的朋友可以参考下

本文实例讲述了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数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

  • python实现xml转json文件的示例代码

    python实现xml转json文件的示例代码

    这篇文章主要介绍了python实现xml转json文件的示例代码,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-12-12
  • 用python打印1~20的整数实例讲解

    用python打印1~20的整数实例讲解

    在本篇内容中小编给大家分享了关于python打印1~20的整数的具体步骤以及实例方法,需要的朋友们参考下。
    2019-07-07
  • python TKinter获取文本框内容的方法

    python TKinter获取文本框内容的方法

    今天小编就为大家分享一篇python TKinter获取文本框内容的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • python基础操作列表推导式

    python基础操作列表推导式

    列表推导式形式较为简洁,是利用其它列表创建新列表的一种方式,它的工作方式类似于for循环,也可以嵌套if条件判断语句,需要的朋友可以参考下
    2023-04-04
  • 用Python编写一个国际象棋AI程序

    用Python编写一个国际象棋AI程序

    在这篇文章中我会介绍这个AI如何工作,每一个部分做什么,它为什么能那样工作起来。你可以直接通读本文,或者去下载代码,边读边看代码。虽然去看看其他文件中有什么AI依赖的类也可能有帮助,但是AI部分全都在AI.py文件中
    2014-11-11
  • 解决pycharm运行时interpreter为空的问题

    解决pycharm运行时interpreter为空的问题

    今天小编就为大家分享一篇解决pycharm运行时interpreter为空的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • python迷宫问题深度优先遍历实例

    python迷宫问题深度优先遍历实例

    这篇文章主要给大家介绍了关于python迷宫问题深度优先遍历的相关资料,深度优先搜索算法(Depth-First-Search),是搜索算法的一种,需要的朋友可以参考下
    2021-06-06
  • python的简单web框架flask快速实现详解

    python的简单web框架flask快速实现详解

    这篇文章主要为大家介绍了python的简单web框架flask快速实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • python 生成空字符串的5种方法

    python 生成空字符串的5种方法

    有时候我们需要生成一个空的字符串,本文就来介绍一下python 生成空字符串的5种方法,包括使用空的单引号或双引号、使用str函数、字符串连接、字符串格式化以及字符串乘法,感兴趣的可以了解一下
    2024-01-01
  • Python文件读写及常用文件的打开方式

    Python文件读写及常用文件的打开方式

    这篇文章主要介绍了Python文件读写及常用文件的打开方式,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09

最新评论