Python使用MongoDB进行模糊查询的实战指南

一只小鹿
一只小鹿
发布时间:2026-10-13 | 2人浏览过
原创

本文详细介绍如何在Python中利用MongoDB进行模糊查询。通过结合re模块与[[wp:a4]]函数,演示了如何对集合中的特定字段进行包含匹配。文章提供了完整的代码示例和结果分析,帮助开发者掌握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中,实现这一功能需要借助正则表达式。

第一种情况:查询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'}

第二种情况:查询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 相关产品和应用

全部下载

专题列表