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

相关推荐
xiaoliuliu1234513 小时前
MONyog-5.6.9-0数据库监控安装步骤详解(附MySQL连接与监控设置教程)
数据库·mysql
dreams_dream13 小时前
Python 的 GIL 是什么?有什么影响?
开发语言·python
小白菜又菜13 小时前
Leetcode 236. Lowest Common Ancestor of a Binary Tree
python·算法·leetcode
多恩Stone13 小时前
【3D-AICG 系列-12】Trellis 2 的 Shape VAE 的设计细节 Sparse Residual Autoencoding Layer
人工智能·python·算法·3d·aigc
yzs8713 小时前
PgSQL的外连接选择率计算
数据库
Loo国昌14 小时前
【AI应用开发实战】09_Prompt工程与模板管理:构建可演进的LLM交互层
大数据·人工智能·后端·python·自然语言处理·prompt
此生只爱蛋14 小时前
【MySQL】变量
数据库·mysql
柒.梧.14 小时前
拆解Spring核心:IOC与AOP底层原理
数据库
遨游xyz14 小时前
Trie树(字典树)
开发语言·python·mysql
重启编程之路14 小时前
AlphaLens Pro V14.0 商业级量化推演终端 | 功能白皮书
python