Python操作mongodb数据库进行模糊查询操作示例
作者:shaomine 发布时间:2024-01-29 03:40:45
本文实例讲述了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程序设计有所帮助。
来源:https://www.cnblogs.com/shaosks/p/5740629.html


猜你喜欢
- 用JDBC实现对MySQL的“增删改查”:import java.sql.Connection;im
- 一、基本概念Reactive X中有几个核心的概念,先来简单介绍一下。1.1、Observable和Observer(可观察对象和观察者)首
- 环境:Zend Studio 8.0Zend Studio是PHP开发者的首选开发工具,其地位相当于微软开发工具中的Visual Studi
- 昨天在网上找资料的时间无意进了一个站,糊里糊涂就进去了,想提权提不起来,后来加上服务商的Q号想社工一下,射了半天得知服务器的安全是绿盟的人给
- -- 创建库CREATE TABLE `rate` ( `uname` VARCHAR (300), `object`
- 借助 org.springframework.ui.Model 对象或 Map 对象将信息传到 springmvc 的页面中需要:jstl
- 大部分新手刚学Django开发的时候默认用的都是SQLite数据库,上线部署的时候,大多用的却是Mysql。那么我们应该如何把数据库从SQL
- 特殊情况有 * ^ : | . \一、单个符号作为分隔符String address="上海\上海市|闵行区\吴中路";
- 我们都知道Jupyter notebook更换主题后看着会很舒服,但是有个问题主题更换后工具栏不显示了。usename$ jt -lAvai
- 大家好,我是辣条。曾经有一个真挚的机会,摆在我面前,但是我没有珍惜,等到失去的时候才后悔莫及,尘世间最痛苦的事莫过于此,如果老天可以再给我一
- 如何保持数据库中原有格式不变:这些问题在论坛里面几乎天天有人问~!其实当在输入信息,然后提交信息的时候,所有内容的格式是没有变的。只是在当提
- hello,小伙伴们大家好,今天给大家介绍的开源项目是Python虚拟环境管理工具,Pipenv是Python官方推荐的包管理工具。可以说,
- 1. views.py定义views视图函数,将数据存入字典。并用压缩为json格式,dumps,并return。import jsonde
- 事务隔离级别设置set global transaction isolation level read committed; //全局的se
- 一、字符串与字节数组?字符串是 Go 语言中最常用的基础数据类型之一,本质上是只读的字符型数组,虽然字符串往往都被看做是一个整体,但是实际上
- 1.概述pyecharts 是百度开源的,适用于数据可视化的工具,配置灵活,展示图表相对美观,顺滑。2.安装python3环境下的安装:pi
- 1.准备工作:工欲善其事必先利其器,因此我们有必要在进行Coding前先配置一个适合我们自己的开发环境,我搭建的开发环境是:操作系统:Ubu
- 1.ROOT_URLCONF = '总路由所在路径(比如untitled.urls)'<===默认情况是这样根路由的路
- 在防止sql注入这些细节出现问题的一般是那些大意的程序员或者是新手程序员,他们由于没有对用户提交过来的数据进行一些必要的过滤,从而导致了给大
- 背景刚入行的同学,看到在SQL语句中出现where 1 = 1这样的条件可能会有所困惑,而长时间这样使用的朋友可能又习以为常。那么,你是否还