Python projection is not working with MongoDB Free Cloud, what is wrong in the query?

mydoc=list(mycol.find({ “pId”:“3020956658” }, {“id1”: 1, “drId”: 0, “name”: 1, “cno”:1, “email”:1, “address”:1,“compid”:0,“comp”:0,“compst”:0,“personalh”:0, “presenth”:0, “pasth”:0,“fh”:1}))

The above line is giving error.
I have tried to follow the order of the different fields.
Kindly help

It will be simpler for us tk help you if you could share the error you are having.

1 Like

The Code:

import pymongo
import time
import datetime
myclient = pymongo.MongoClient("mongodb+srv://<dbname>?retryWrites=true&w=majority")
mydb = myclient["hcd"]
mycol = mydb["c61"]
ts1 = time.time()
mydoc=list(mycol.find({ "pId":"6993341507" }, {"compid" : 0, "comp" :1, "compst":1}))
# )) 
# {"id1": 1, "drId": 0, "name": 1, "cno":1, "email":1, "address":1,"compid":0,"comp":0,"compst":0,"personalh":0, "presenth":0, "pasth":0,"fh":1
ts2 = time.time()
td= ts2-ts1
print('The difference is approx. %s seconds' % td)

The Output:

2021-03-16 16:07:33.998910
Traceback (most recent call last):
  File "E:\nm21\", line 12, in <module>
    mydoc=list(mycol.find({ "pId":"6993341507" }, {"compid" : 0, "comp" :1, "compst":1}))
  File "C:\Python\Python39\lib\site-packages\pymongo\", line 1207, in next
    if len(self.__data) or self._refresh():
  File "C:\Python\Python39\lib\site-packages\pymongo\", line 1124, in _refresh
  File "C:\Python\Python39\lib\site-packages\pymongo\", line 999, in __send_message
    response = client._run_operation_with_response(
  File "C:\Python\Python39\lib\site-packages\pymongo\", line 1368, in _run_operation_with_response
    return self._retryable_read(
  File "C:\Python\Python39\lib\site-packages\pymongo\", line 1471, in _retryable_read
    return func(session, server, sock_info, slave_ok)
  File "C:\Python\Python39\lib\site-packages\pymongo\", line 1360, in _cmd
    return server.run_operation_with_response(
  File "C:\Python\Python39\lib\site-packages\pymongo\", line 136, in run_operation_with_response
  File "C:\Python\Python39\lib\site-packages\pymongo\", line 167, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response,
pymongo.errors.OperationFailure: Cannot do inclusion on field comp in exclusion projection, full error: {'operationTime': Timestamp(1615891048, 1), 'ok': 0.0, 'errmsg': 'Cannot do inclusion on field comp in exclusion projection', 'code': 31253, 'codeName': 'Location31253', '$clusterTime': {'clusterTime': Timestamp(1615891048, 1), 'signature': {'hash': b'\xfeu\xdb\xa7c\xfa=\xf20\xed`\xb6s\xd3xsm\xcaV|', 'keyId': 6920164380919201795}}}

Hi @sps,

When you do a projection, you can either do an exclusion filter, with fields that have a value of 0, or an inclusion filter, with fields that have a value of 1. The exception to this is the _id field, which is always included unless it’s specifically excluded.

I’m not sure exactly what you’re trying to do, but I think one of the following may do what you want:

# Inclusion filter (any fields not listed are removed, except for _id):
mydoc=list(mycol.find({ "pId":"3020956658" }, {"id1": 1, "name": 1, "cno":1, "email":1, "address":1, "fh":1}))

# Inclusion filter that excludes _id:
mydoc=list(mycol.find({ "pId":"3020956658" }, {"id1": 1, "name": 1, "cno":1, "email":1, "address":1, "fh":1, "_id": 0}))

# Exclusion filter (any fields listed are removed):
mydoc=list(mycol.find({ "pId":"3020956658" }, {"drId": 0,"compid":0,"comp":0,"compst":0,"personalh":0, "presenth":0, "pasth":0}))

Some more examples are in the MongoDB tutorial

I hope this helps!


I have clearly understood. It worked perfectly.
I have started working with mongodb practically, after mid November, 2020.
I have got enormous help and support from mongodb university and people like you.
Thanks a lot.
If I have any more doubts regarding any other topic I will definitely post here again. The terms inclusion and exclusion are self explanatory but got confused somehow about the syntax. Thanks for the elaboration again.


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.