python mongodb 查询忽略大小写

python 复制代码
import pymongo
import random
import string
from pymongo.collation import Collation
random.seed(10)
letters = string.ascii_lowercase
upper = string.ascii_uppercase


class MongoDBServer():
    def __init__(self,database,collation) -> None:
        self.client = pymongo.MongoClient('mongodb://ellis:ellischen@192.168.214.133:32000/')
        self.database = self.client[database]
        

        if collation not in self.database.list_collection_names():
            self.database.create_collection(collation,
                                    collation=Collation(locale='en'))
        
        
        self.colleceion = self.database[collation]
        
    def insert_many(self,documents):
        self.colleceion.insert_many(documents)


server = MongoDBServer('test','collation')

documents= []

for i in range(10):
    rand_letters = random.choices(letters,k=2)
    upper_letters = random.choices(upper,k=2)
    value = {"name":''.join(rand_letters)+''.join(upper_letters),"age":100}
    documents.append(value)



server.insert_many(documents)

print(server.client['test']['collation'].find({"name":"ADYH"}).collation({ "locale": "en", "strength": 1 }).explain()['executionStats'])

最主要的要先创建collection并指定collation,查询时候也带上这个参数

关于这个参数的解释
https://pymongo.readthedocs.io/en/stable/examples/collations.html

相关推荐
njsgcs1 分钟前
json转excel python pd
python·json·excel·pd
fredinators7 分钟前
数据库flask访问
数据库·oracle·flask
向葭奔赴♡14 分钟前
Spring Boot 分模块:从数据库到前端接口
数据库·spring boot·后端
一晌小贪欢28 分钟前
Python爬虫第7课:多线程与异步爬虫技术
开发语言·爬虫·python·网络爬虫·python爬虫·python3
JosieBook43 分钟前
【数据库】时序数据库选型指南:在大数据与工业4.0时代,为何 Apache IoTDB 成为智慧之选?
大数据·数据库·时序数据库
程序员三明治44 分钟前
详解Redis锁误删、原子性难题及Redisson加锁底层原理、WatchDog续约机制
java·数据库·redis·分布式锁·redisson·watchdog·看门狗
chenzhou__1 小时前
MYSQL学习笔记(个人)(第十五天)
linux·数据库·笔记·学习·mysql
一只自律的鸡2 小时前
【MySQL】第二章 基本的SELECT语句
数据库·mysql
yanxing.D3 小时前
OpenCV轻松入门_面向python(第六章 阈值处理)
人工智能·python·opencv·计算机视觉
liliangcsdn3 小时前
如何使用python创建和维护sqlite3数据库
数据库·sqlite