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://pss:nm22@cluster0.uxfb5.mongodb.net/<dbname>?retryWrites=true&w=majority")
mydb = myclient["hcd"]
mycol = mydb["c61"]
x=datetime.datetime.now()
print(x)
ts1 = time.time()
print(ts1)
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()
print(ts2)
td= ts2-ts1
print('The difference is approx. %s seconds' % td)
print(mydoc)

The Output:

2021-03-16 16:07:33.998910
1615891053.9989104
Traceback (most recent call last):
  File "E:\nm21\readmongoc61.py", line 12, in <module>
    mydoc=list(mycol.find({ "pId":"6993341507" }, {"compid" : 0, "comp" :1, "compst":1}))
  File "C:\Python\Python39\lib\site-packages\pymongo\cursor.py", line 1207, in next
    if len(self.__data) or self._refresh():
  File "C:\Python\Python39\lib\site-packages\pymongo\cursor.py", line 1124, in _refresh
    self.__send_message(q)
  File "C:\Python\Python39\lib\site-packages\pymongo\cursor.py", line 999, in __send_message
    response = client._run_operation_with_response(
  File "C:\Python\Python39\lib\site-packages\pymongo\mongo_client.py", line 1368, in _run_operation_with_response
    return self._retryable_read(
  File "C:\Python\Python39\lib\site-packages\pymongo\mongo_client.py", line 1471, in _retryable_read
    return func(session, server, sock_info, slave_ok)
  File "C:\Python\Python39\lib\site-packages\pymongo\mongo_client.py", line 1360, in _cmd
    return server.run_operation_with_response(
  File "C:\Python\Python39\lib\site-packages\pymongo\server.py", line 136, in run_operation_with_response
    _check_command_response(
  File "C:\Python\Python39\lib\site-packages\pymongo\helpers.py", 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!

2 Likes

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.

2 Likes

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