$elemMatch not working with Pymongo

Hello!
I’ve been trying to get my query to work but it always fails, so I’m guessing I have a syntax error of some sort. I basically want to check if a specific userid exists, so in my case my query should return True, but it returns false. Can anyone help?

My data:
image

My code:
bool = mydb.mycollection.find({"buttons": {"$elemMatch": {"userid": 198832506034716672}}}, limit = 1) == 1

Hello @Lily_B, welcome to the MongoDB Community forum!

The following query will work in PyMongo:

for doc in mydb.mycollection.find( { "buttons.userid": 67890 }, limit = 1 ):
    print(doc)

It will print the first matching document, as you have specified the limit = 1 option. The find method returns a cursor, which will be empty if there is no match.

Since you are trying to check if there are any documents exist, you can use the following query (instead of); it prints the count of matching documents.

print(mydb.mycollection.count_documents({ "buttons.userid": 67890 }))