我目前在FastAPI应用程序中使用Motor客户端(v3.1.1)。当尝试实现分页时,以下代码可以正常工作:
client = motor.motor_asyncio.AsyncIOMotorClient('mongodb://<my-connection-string>/')
database = client.test_database
collection = database.test_collection
pipeline = [
{ "$facet":
{"items":
[{ "$match": { }},
{"$sort": {"_id": -1}},
{ "$skip": pageskip },
{ "$limit": pagesize },
],
"totalCount": [{ "$count": "count" }]}}
]
cursor = collection.aggregate(pipeline)
然而,在一定数量的页面后,我得到以下错误:
raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: Sort exceeded memory limit of 104857600 bytes, but
did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt
in., full error: {'ok': 0.0, 'errmsg': 'Sort exceeded memory limit of 104857600
bytes, but did not opt in to external sorting. Aborting operation. Pass
allowDiskUse:true to opt in.', 'code': 292,
'codeName':'QueryExceededMemoryLimitNoDiskUseAllowed'}
我尝试直接在聚合中使用[allowDiskUse][1],但随后我得到以下内容:
raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: Unrecognized pipeline stage name: 'allowDiskUse',
full error: {'ok': 0.0, 'errmsg': "Unrecognized pipeline stage name: 'allowDiskUse'",
'code': 40324, 'codeName': 'Location40324'}
如果有人知道如何实现allowDiskUse与电机客户端提前感谢…
将其作为标志添加到aggregate
方法中:
cursor = collection.aggregate(pipeline, allowDiskUse=True)