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

相关推荐
寂寞旅行2 小时前
向量数据库Milvus的使用
数据库·milvus
闻哥3 小时前
Redis事务详解
java·数据库·spring boot·redis·缓存·面试
道亦无名3 小时前
aiPbMgrSendAck
java·网络·数据库
CeshirenTester3 小时前
9B 上端侧:多模态实时对话,难点其实在“流”
开发语言·人工智能·python·prompt·测试用例
Starry_hello world3 小时前
Python (2)
python
ID_180079054734 小时前
Python爬取京东商品库存数据与价格监控
jvm·python·oracle
-To be number.wan5 小时前
Python数据分析:时间序列数据分析
开发语言·python·数据分析
Faker66363aaa5 小时前
YOLO13-C3K2-AdditiveBlock:水果质量智能检测系统_3
python
2401_828890646 小时前
实现扩散模型 Stable Diffusion - MNIST 数据集
人工智能·python·深度学习·stable diffusion
面向对象World6 小时前
正点原子Mini Linux 4.3寸800x480触摸屏gt115x驱动
linux·服务器·数据库