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

Python 如何解决这个问题pymongo.errors.OperationFailure?,python,mongodb,pymongo,Python,Mongodb,Pymongo,我正试图在我的数据库中进行标题搜索,但我做不到 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"] my

我正试图在我的数据库中进行标题搜索,但我做不到

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)

这将搜索所有索引字段并返回匹配字段。有关的详细信息,请参阅。

$search是聚合管道操作符。有关正确用法,请参阅。无法在查找查询中指定它。

我也尝试过,但是
引发操作失败(msg%errmsg,code,response,pymongo.errors.OperationFailure:text索引$text查询需要,完整错误:{'operationTime':时间戳(1601927775,17),'ok':0.0,'errmsg':'text index required for$text query','code':27,'codeName':'IndexNotFound','$clusterTime':{'clusterTime':Timestamp(1601927775,17),'signature':{'hash':b'ZUV\xbez\xedh\xb6\xaa\xb3\xcd)$\xa2\xee\xda\xfbu\x85\xe7,'keyId','6869109962937729027}
@fungalspores,您需要在标题字段上创建索引。我遇到了新的问题
raisetypeerror(“'Collection'对象不可调用。如果您想”TypeError:“Collection”对象不可调用。如果您想调用“Collection”对象上的“createIndex”方法,则该方法失败,因为不存在此类方法。
@fungalspores这是因为pymongo具有
创建索引
函数,而不是createIndex。我认为这是lasr的问题
引发TypeError(“如果未指定方向,”TypeError:如果未指定方向,则键或列表必须是列表的实例