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

相关推荐
就叫飞六吧11 小时前
MySQL不停机迁移完全指南
数据库·mysql
猎人everest12 小时前
Windows系统Redis(8.2.2)安装与配置完整教程
数据库·windows·redis
苦学编程的谢12 小时前
Redis_10_Zset
数据库·redis·缓存
q***420512 小时前
在Spring Boot项目中使用MySQL数据库
数据库·spring boot·mysql
郝开12 小时前
Spring Boot 2.7.18(最终 2.x 系列版本)3 - 枚举规范定义:定义基础枚举接口;定义枚举工具类;示例枚举
spring boot·后端·python·枚举·enum
钅日 勿 XiName12 小时前
一小时速通Pytorch之自动梯度(Autograd)和计算图(Computational Graph)(二)
人工智能·pytorch·python
故林丶12 小时前
【Django】Django笔记
python·django
我系真滴菜12 小时前
SQLiteStudio基本操作教程
数据库
q***498613 小时前
在Django中安装、配置、使用CKEditor5,并将CKEditor5录入的文章展现出来,实现一个简单博客网站的功能
数据库·django·sqlite
q***180613 小时前
如何使用C#与SQL Server数据库进行交互
数据库·c#·交互