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

相关推荐
A XMan.13 分钟前
域名Whois信息查询V2版API接入指南
数据库
heimeiyingwang14 分钟前
【架构实战】可观测性体系:从监控到全链路追踪
网络·数据库·架构
网管NO.118 分钟前
SQL 日期函数全套精讲!时间格式化、日期加减、年月日提取,做日报周报直接套用
数据库·sql
杨云龙UP25 分钟前
Linux 根分区被日志吃满?一次 58G Broker 日志清理实战_2026-05-20
linux·运维·服务器·数据库·hdfs·apache
sdk大全28 分钟前
Studio 3T for MongoDB 2025.13.0
数据库·mongodb
码农阿豪29 分钟前
平替MongoDB:金仓多模数据库助力电子证照国产化实践
数据库·mongodb
罗超驿30 分钟前
22.深入剖析JDBC架构:从原生API到企业级数据交互核心
java·数据库·mysql·面试
易辰君35 分钟前
【数据库】MongoDB深度解析与Python操作指南:从安装到实战操作全覆盖
数据库·mongodb
一直有一个ac的梦想39 分钟前
cmu15445 2025fall lec 18 transactions with two-phase lock
java·开发语言·数据库
身如柳絮随风扬1 小时前
Redis 集群脑裂深度剖析:成因、危害与防丢失策略
数据库