How to get a result from $elemMatch (example: True/False)

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.

1 Like

Hi @SomeoneOnTheInternet,

Welcome to MongoDB community!

Can you elaborate more on the requirements, maybe even provide a pseudo code or the sample doc before and after the query.

Best
Pavel

1 Like

for an example I’m going to give the question with pymongo as thats what I’m using on my app
so

isfriendsorno = user_data_collection.find_one(
                        {
                            "friends": {
                                "$elemMatch": {
                                    "friend_id": ObjectId(str(spuseruserid))
                                }
                            }
                        })

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.

But when I try it with a search query like:

isfriendsorno = user_data_collection.find_one({"_id": ObjectId(str(curuseruserid))},
                        {
                            "friends": {
                                "$elemMatch": {
                                    "friend_id": ObjectId(str(spuseruserid))
                                }
                            }
                        })

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.

Hi @SomeoneOnTheInternet,

I think your actual problem is that the second condition is not inside the condition object but in projection part.

Try the following statement:

isfriendsorno = user_data_collection.find_one({"_id": ObjectId(str(curuseruserid)),
                      "friends": {
                                "$elemMatch": {
                                    "friend_id": ObjectId(str(spuseruserid))
                                }
                            }}  )

Notice that the command now have a single condition object with an “and” between _id and friends.

Let me know if that works for you.

Thanks
Pavel

1 Like

Hi @SomeoneOnTheInternet,

Additionally if the friends array only have ids you can use:

isfriendsorno = user_data_collection.find_one({"_id": ObjectId(str(curuseruserid)),
                      "friends.friend_id": ObjectId(str(spuseruserid))
                                }  )

Thanks
Pavel

1 Like

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