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

相关推荐
wno7043 小时前
postgresSQL压缩版安装与基本命令
数据库·postgresql
张张123y3 小时前
后端第一课:输入 URL 后发生了什么?我用手写 Socket 把全链路跑通了
python·websocket·ai·socket·url·后端开发
聚美智数5 小时前
智能结构化识别-图片结构解析-图像结构化解析-照片结构化解析
android·java·数据库
瀚高PG实验室5 小时前
瀚高数据库常用系统表查询入门指南(附常用SQL)
数据库·sql·postgresql·瀚高数据库
全栈弄潮儿6 小时前
Python实战第7期:字典与集合
后端·python·编程语言
ben9518chen6 小时前
mysql数据库基本操作
数据库·mysql
2603_965148116 小时前
电商选品调研:通过关键词搜索API获取市场热销商品,结合商品详情API分析竞品卖点与定价
开发语言·数据库·人工智能·python·信息可视化
土豆.exe6 小时前
nf_tables 的 verdict 被读了两次:一个十年未闭合的位域错配
java·数据库·网络安全·缓存
xixiaoyunya7 小时前
Python 异步编程入门:用 asyncio 轻松实现并发任务
开发语言·python·php
缓慢更新7 小时前
表单引擎的存储建模:宽表与文档库的取舍
运维·服务器·网络·数据库·低代码·oracle·适配器模式