激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

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

服務(wù)器之家 - 腳本之家 - Python - Python操作mongodb數(shù)據(jù)庫進行模糊查詢操作示例

Python操作mongodb數(shù)據(jù)庫進行模糊查詢操作示例

2021-03-03 00:04shaomine Python

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

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

?
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]

假設(shè)mongodb數(shù)據(jù)庫中school 集合中有一些數(shù)據(jù)記錄

?
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" } }

現(xiàn)在要對students中comments的數(shù)據(jù)進行模糊查詢, python中模糊查詢要借助正則表達式:

1、查詢comments中包含"abc"的記錄:

?
1
2
for u in db.school.find({'students.comments':re.compile('abc')}):
  print u

結(jié)果如下:

{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

結(jié)果如下:

{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()函數(shù)

希望本文所述對大家Python程序設(shè)計有所幫助。

原文鏈接:https://www.cnblogs.com/shaosks/p/5740629.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄污视频在线看 | 久久久久久久久久91 | 欧美黄色免费视频 | 久草在线综合 | 最新久久免费视频 | 国内精品伊人久久久久网站 | 天使萌一区二区三区免费观看 | 黄色午夜剧场 | 亚洲第九十九页 | www成人在线观看 | 99在线精品视频免费观看20 | 黄色片网站在线免费观看 | 在线免费观看欧美 | 黄色大片免费看 | 久久福利小视频 | 美女毛片儿 | 99久久久免费视频 | 国产精品自拍av | 国产欧美亚洲精品a | av在线直播观看 | 91久久国产露脸精品免费 | 色综合久久99| 欧美精品亚洲人成在线观看 | 欧美一区在线观看视频 | 九九视频精品在线观看 | av电影直播 | 国产精品色综合 | 久色porn | 午夜视频在线 | 欧美久久久久久久久 | 九九热精品视频在线播放 | 噜噜噜在线| 国产一区精品在线观看 | 蜜桃久久一区二区三区 | 国产日本在线 | 免费看搡女人无遮挡的视频 | 午夜91视频| 一级美女大片 | 亚洲福利视频52 | 7777网站| 成人黄色在线免费观看 |