我在 Atlas 数据库上使用 pymongo 时不断收到此错误:
raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: Cannot do exclusion on field description in inclusion projection, full error: {'ok': 0.0, 'errmsg': 'Cannot do exclusion on field description in inclusion projection'
尝试运行此代码时:
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
uri = "mongodb+srv://USER:[email protected]/?retryWrites=true&w=majority"
client = MongoClient(uri, server_api=ServerApi('1'))
db = client['DB_NAME']
i = db['COLLECTION_NAME']
x = i.find({}, {'_id': 0, 'name': 1, 'email': 0, 'phone': 0})
for a in x:
print(a)
如果有人可以告诉我发生了什么以及如何解决它,那将非常有帮助。
您不能混合包含和排除预测,除了
_id
。
要么包含某些东西,然后排除其他所有内容;或排除某些内容,其他所有内容都包含在内。
所以就这样做:
x = i.find({}, {'_id': 0, 'name': 1})