pymongo的正則查詢


一:數據樣式

/* 1 */
{
    "_id" : ObjectId("5ea4fde22a89d7c2fc456ad4"),
    "name" : "陝西西安",
    "location" : "新城區"
}

/* 2 */
{
    "_id" : ObjectId("5ea4fe152a89d7c2fc456b19"),
    "name" : "內蒙古鄂爾多斯",
    "location" : "烏蘭鎮"
}

/* 3 */
{
    "_id" : ObjectId("5ea4fe292a89d7c2fc456b3b"),
    "name" : "內蒙古巴彥淖爾",
    "location" : "彩虹鎮"
}

/* 4 */
{
    "_id" : ObjectId("5ea4fe6c2a89d7c2fc456b9c"),
    "name" : "廣西桂林市",
    "location" : "七星區"
}

 需求:查詢名字包含內蒙古的所有數據

    def find_by_regex(self):
        if self.connect_result:
            result = self.db["test1"].find({"name":{"$regex":""}})
            for i in result:
                print(i)

# 結果
{'_id': ObjectId('5ea4fe152a89d7c2fc456b19'), 'name': '內蒙古鄂爾多斯', 'location': '烏蘭鎮'}
{'_id': ObjectId('5ea4fe292a89d7c2fc456b3b'), 'name': '內蒙古巴彥淖爾', 'location': '彩虹鎮'}

 

 

 

 

 

 

# TODO


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM