我该如何解决这个问题pymongo.errors.OperationFailure?

How I can solve this problem pymongo.errors.OperationFailure?

我想在我的数据库中进行标题搜索,但我不能。

import pymongo

myclient = pymongo.MongoClient("mongodb+srv://LOGIN:PASS@cluster0.ye4cx.mongodb.net/info?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE")
mydb = myclient["info"]
mycol = mydb["comics"]

find = mycol.find({"title": {"$search": "68"}})

for f in find:
    print(f)

但是我得到这个错误

raise OperationFailure(msg % errmsg, code, response,
pymongo.errors.OperationFailure: unknown operator: $search, full error: {'operationTime': Timestamp(1601923289, 13), 'ok': 0.0, 'errmsg': 'unknown operator: $search', 'code': 2, 'codeName': 'BadValue', '$clusterTime': {'clusterTime': Timestamp(1601923289, 13), 'signature': {'hash': b'\x82\x91\xc5\xd4r\xd6\xbf\xbc\x13i\xe5\x83b\xd2\x9eUv\xb8\x89/', 'keyId': 6869109962937729027}}}

您需要使用 $text 运算符才能使其正常工作。不要忘记在标题字段上创建索引。

import pymongo
from pymongo import TEXT

myclient = pymongo.MongoClient("mongodb+srv://LOGIN:PASS@cluster0.ye4cx.mongodb.net/info?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE")
mydb = myclient["info"]
mycol = mydb["comics"]

#Creating index on title
mycol.create_index([('title', TEXT)], default_language='english')

find = mycol.find({"$text": {"$search": "68"}})

for f in find:
    print(f)

这将搜索所有索引字段和 return 匹配字段。在 official documentation.

上查看更多详细信息

$search 是聚合管道运算符。请参阅 https://docs.atlas.mongodb.com/reference/atlas-search/query-syntax/#query-syntax-ref 了解正确用法。不能在查找查询中指定。