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

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

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
#創建連接
#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久久免费观看 | 操比软件| 52zfl宅福利yxpjw | 草莓视频网站18勿进 | 国内自拍成人网在线视频 | 欧洲另类一二三四区 | 国产精品久久久久影院色老大 | 男女做性视频 | 性啪啪chinese东北女人 | 午夜小视频免费观看 | 能免费观看的韩剧 | 欧美成人第一页 | 黄在线观看www免费看 | 成人在线视频播放 | 久久精品一区二区三区资源网 | 欧美一区二区三区高清不卡tv | 日韩日日日 | 国产91精品久久久久久 | 天天操丝袜 | 国产亚洲小视频 | 日韩欧美高清一区 | 日韩毛片基地一区二区三区 | 亚洲色图2 | 99ri在线视频网 | 99久久999久久久综合精品涩 | 国产欧美一区二区三区免费 | 性夜夜春夜夜爽AA片A | 欧美精品国产一区二区 | 久久综合给会久久狠狠狠 | 91九色porn偷拍在线 | 亚洲性视频在线观看 | 婷婷久久热99在线精品 | 久久re视频这里精品一本到99 | 亚洲AV无码国产精品色午夜情 |