一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python操作mongodb數據庫進行模糊查詢操作示例

Python操作mongodb數據庫進行模糊查詢操作示例

2021-03-03 00:04shaomine Python

這篇文章主要介紹了Python操作mongodb數據庫進行模糊查詢操作,結合實例形式分析了Python連接MongoDB數據庫及使用正則表達式進行模糊查詢的相關操作技巧,需要的朋友可以參考下

本文實例講述了Python操作mongodb數據庫進行模糊查詢操作。分享給大家供大家參考,具體如下:

?
1
2
3
4
5
6
7
8
9
10
# -*- coding: utf-8 -*-
import pymongo
import re
from pymongo import MongoClient
#創(chuàng)建連接
#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 集合中有一些數據記錄

?
1
2
3
4
5
6
7
8
9
10
11
{ "_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"的記錄:

?
1
2
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"的記錄:

?
1
2
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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费在线视频观看 | 女生被草 | 青青草在观免费 | 亚洲视频日韩 | 99久女女精品视频在线观看 | 欧美乱妇高清无乱码视频在线 | 大乳一级一区二区三区 | 毛片应用| 欧美成人一区二区三区 | 欧洲男同直粗无套播放视频 | 咪咪爱991 | 粉嫩尤物在线456 | 午夜福利在线观看6080 | 久久精麻豆亚洲AV国产品 | 91视频免费观看网站 | 欧美一级片免费 | chinesemature丰满成熟 | 天天久久综合网站 | 91久久国产青草亚洲 | 国产情侣偷国语对白 | 国产日韩免费视频 | 亚洲国产情侣一区二区三区 | 满城尽带黄金甲大胸片 | 亚洲天堂男人网 | 波多野给衣一区二区三区 | 亚洲品质自拍视频 | 性柔术18性13处交 | 欧美一级片观看 | 午夜精品久久久久久久99 | 国产在线观看人成激情视频 | 大学生宿舍飞机china free | 亚洲国产精品日韩高清秒播 | 免费在线视频一区 | 日本在线视频播放 | 国产成人精品高清在线 | 3344在线看片 | 视频在线观看一区二区三区 | 亚洲男人天堂av | 精品在线观看一区 | 狠狠五月天中文字幕 | 亚洲乱亚洲23p女 |