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

相关推荐
九河_3 分钟前
解决pip install gym==0.19.0安装失败问题
开发语言·python·pip·gym
Elias不吃糖1 小时前
MYSQL指令合集
数据库·mysql
iamohenry2 小时前
古早味的心理咨询聊天机器人
python·自然语言处理
!chen3 小时前
解决 Oracle 监听外网 IP
数据库·tcp/ip·oracle
LBuffer4 小时前
破解入门学习笔记题四十六
数据库·笔记·学习
chase。4 小时前
关于 nvidia-smi: no devices were found 解决方案
服务器·数据库·postgresql
几何心凉4 小时前
openGauss:多核时代企业级数据库的性能与高可用新标杆
前端·数据库·数据库开发
瑞思蕊萌5 小时前
redis实战篇--商品缓存模块
数据库·redis·缓存
Blossom.1186 小时前
移动端部署噩梦终结者:动态稀疏视觉Transformer的量化实战
java·人工智能·python·深度学习·算法·机器学习·transformer
AiXed6 小时前
PC微信协议之AES-192-GCM算法
前端·数据库·python