一:數據樣式
/* 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
