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文章来源地址https://www.toymoban.com/news/detail-589524.html
文章来源:https://www.toymoban.com/news/detail-589524.html
到了这里,关于python mongodb 查询忽略大小写的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!