Search code examples
pythondatabasepymongomongodb-atlas

pymongo raise OperationFailure while accessing data


I keep getting this error while using pymongo on my Atlas Database:

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'

while trying to run this code:


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)

If someone could tell me what is happening and how to fix it, that would be very helpful.


Solution

  • You can't mix inclusion and exclusion projections, except for _id.

    Either include something, and everything else is excluded; or exclude something, and everthing else is included.

    so just do:

    x = i.find({}, {'_id': 0, 'name': 1})