How to use $elemMatch in an if statement? I tried declaring my find value to a variable and made an if-else statement using None (my app is in python, None basically means Null). It works but it doesn’t work if I give it a query as it returns the _id of the query instead so its not a None.
So how can I write an if statement in MongoDB using a queried $elemMatch? (you can answer this question in whichever programming language you want to use, even shell would be great. I just want to get the logic of it. Thanks.
returns a query if the user is found and returns a None if it isn’t so its super easy to use it in an if-else statement, but the problem is I’m building a social media app and as this doesn’t have a user to search for (the search query or whatever its called) it returns a general data looking if any other user is friends with the user the friend request is sent to even if the current user isn’t friends with the other user and so it causes problems in the app like showing people who aren’t friends with some users as friends with those users.
if the other user isn’t friends with the current logged in user this assigns the value of the current logged in users _id to the variable so I cant use it in an if-else statement using “None”.
example of what it assigns to the variable if the value is isn’t found in the array when I print it out to see — {’_id’: ObjectId(“whatever the id is”)}}
I tried tuning it into a string with str(), than I tried to literally turn it into a string with quotes and put the value in a variable inside the quotes, I tried to access it with square brackets, I tried to convert it into another complete user data if its found and just access the part I need later but none of them didn’t work, any help would be appreciated.
PS: I’m kind of new to MongoDB and English isn’t my first language so pardon me if I explained it bad.