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

相关推荐
2301_8135995525 分钟前
Go语言怎么做秒杀系统_Go语言秒杀系统实战教程【实用】
jvm·数据库·python
--fancy4 小时前
股票预测情感分析研究案例分析
python
shughui5 小时前
PyCharm 完整教程(旧版本卸载+旧/新版本下载安装+基础使用,2026最新版附安装包)
ide·python·pycharm
NCIN EXPE5 小时前
redis 使用
数据库·redis·缓存
MongoDB 数据平台5 小时前
为编码代理引入 MongoDB 代理技能和插件
数据库·mongodb
极客on之路5 小时前
mysql explain type 各个字段解释
数据库·mysql
代码雕刻家5 小时前
MySQL与SQL Server的基本指令
数据库·mysql·sqlserver
lThE ANDE5 小时前
开启mysql的binlog日志
数据库·mysql
小糖学代码5 小时前
LLM系列:1.python入门:15.JSON 数据处理与操作
开发语言·python·json·aigc
yejqvow125 小时前
CSS如何控制placeholder文字的颜色_使用--placeholder伪元素
jvm·数据库·python