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