欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年06月09日 12:47:33   作者:shaomine  
這篇文章主要介紹了Python操作mongodb數(shù)據(jù)庫進(jìn)行模糊查詢操作,結(jié)合實(shí)例形式分析了Python連接MongoDB數(shù)據(jù)庫及使用正則表達(dá)式進(jìn)行模糊查詢的相關(guān)操作技巧,需要的朋友可以參考下

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

# -*- 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ù)記錄

{ "_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)在要對(duì)students中comments的數(shù)據(jù)進(jìn)行模糊查詢, python中模糊查詢要借助正則表達(dá)式:

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

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

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ù)

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

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

相關(guān)文章

  • python實(shí)現(xiàn)xml轉(zhuǎn)json文件的示例代碼

    python實(shí)現(xiàn)xml轉(zhuǎn)json文件的示例代碼

    這篇文章主要介紹了python實(shí)現(xiàn)xml轉(zhuǎn)json文件的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • 用python打印1~20的整數(shù)實(shí)例講解

    用python打印1~20的整數(shù)實(shí)例講解

    在本篇內(nèi)容中小編給大家分享了關(guān)于python打印1~20的整數(shù)的具體步驟以及實(shí)例方法,需要的朋友們參考下。
    2019-07-07
  • python TKinter獲取文本框內(nèi)容的方法

    python TKinter獲取文本框內(nèi)容的方法

    今天小編就為大家分享一篇python TKinter獲取文本框內(nèi)容的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python基礎(chǔ)操作列表推導(dǎo)式

    python基礎(chǔ)操作列表推導(dǎo)式

    列表推導(dǎo)式形式較為簡潔,是利用其它列表創(chuàng)建新列表的一種方式,它的工作方式類似于for循環(huán),也可以嵌套if條件判斷語句,需要的朋友可以參考下
    2023-04-04
  • 用Python編寫一個(gè)國際象棋AI程序

    用Python編寫一個(gè)國際象棋AI程序

    在這篇文章中我會(huì)介紹這個(gè)AI如何工作,每一個(gè)部分做什么,它為什么能那樣工作起來。你可以直接通讀本文,或者去下載代碼,邊讀邊看代碼。雖然去看看其他文件中有什么AI依賴的類也可能有幫助,但是AI部分全都在AI.py文件中
    2014-11-11
  • 解決pycharm運(yùn)行時(shí)interpreter為空的問題

    解決pycharm運(yùn)行時(shí)interpreter為空的問題

    今天小編就為大家分享一篇解決pycharm運(yùn)行時(shí)interpreter為空的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python迷宮問題深度優(yōu)先遍歷實(shí)例

    python迷宮問題深度優(yōu)先遍歷實(shí)例

    這篇文章主要給大家介紹了關(guān)于python迷宮問題深度優(yōu)先遍歷的相關(guān)資料,深度優(yōu)先搜索算法(Depth-First-Search),是搜索算法的一種,需要的朋友可以參考下
    2021-06-06
  • python的簡單web框架flask快速實(shí)現(xiàn)詳解

    python的簡單web框架flask快速實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了python的簡單web框架flask快速實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • python 生成空字符串的5種方法

    python 生成空字符串的5種方法

    有時(shí)候我們需要生成一個(gè)空的字符串,本文就來介紹一下python 生成空字符串的5種方法,包括使用空的單引號(hào)或雙引號(hào)、使用str函數(shù)、字符串連接、字符串格式化以及字符串乘法,感興趣的可以了解一下
    2024-01-01
  • Python文件讀寫及常用文件的打開方式

    Python文件讀寫及常用文件的打開方式

    這篇文章主要介紹了Python文件讀寫及常用文件的打開方式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09

最新評(píng)論