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

相关推荐
程序员爱钓鱼1 小时前
Python编程实战 · 基础入门篇 | 条件判断 if...else
后端·python
zz-zjx1 小时前
MySQL 开源主从复制实战指南(SRE 可靠性优先版)
数据库·mysql·开源
程序员爱钓鱼1 小时前
Python编程实战 · 基础入门篇 | 循环语句 for / while
后端·python
飞翔的佩奇7 小时前
【完整源码+数据集+部署教程】【天线&水】舰船战舰检测与分类图像分割系统源码&数据集全套:改进yolo11-repvit
前端·python·yolo·计算机视觉·数据集·yolo11·舰船战舰检测与分类图像分割系统
Microsoft Word8 小时前
向量数据库与RAG
数据库·人工智能·向量数据库·rag
木头左8 小时前
最大回撤约束下ETF多因子动态止盈参数校准方案
python
汤姆yu8 小时前
2026版基于python的协同过滤音乐推荐系统
开发语言·python
汤姆yu8 小时前
基于python的电子商务管理系统
开发语言·python
may_一一8 小时前
pycharm\python 安装下载
ide·python·pycharm
后台开发者Ethan9 小时前
FastAPI之 Python的类型提示
python·fastapi·ai编程