Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB return True if document exists

I want to return true if a userID already exists and false otherwise from my collection.I have this function but it always returns True.

def alreadyExists(newID):     if db.mycollection.find({'UserIDS': { "$in": newID}}):         return True     else:         return False 

How could I get this function to only return true if a user id already exists?

like image 912
user94628 Avatar asked Aug 06 '14 14:08

user94628


People also ask

How do you check if a document exist in MongoDB?

In MongoDB, we can check the existence of the field in the specified collection using the $exists operator. When the value of $exists operator is set to true, then this operator matches the document that contains the specified field(including the documents where the value of that field is null).

Which of the following method returns true if the cursor has documents?

Which of the following method returns true if the cursor has documents? Explanation: hasNext() returns true if the cursor returned by the db.

What is MongoDB elemMatch?

The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

How do I query NULL values in MongoDB?

MongoDB fetch documents containing 'null' If we want to fetch documents from the collection "testtable" which contains the value of "interest" is null, the following mongodb command can be used : >db. testtable. find( { "interest" : null } ).


2 Answers

Note: This answer is outdated. More recent versions of MongoDB can use the far more efficient method db.collection.countDocuments. See the answer by Xavier Guihot for a better solution.

find doesn't return a boolean value, it returns a cursor. To check if that cursor contains any documents, use the cursor's count method:

if db.mycollection.find({'UserIDS': { "$in": newID}}).count() > 0 

If newID is not an array you should not use the $in operator. You can simply do find({'UserIDS': newID}).

like image 77
Philipp Avatar answered Sep 20 '22 10:09

Philipp


Starting Mongo 4.0.3/PyMongo 3.7.0, we can use count_documents:

if db.collection.count_documents({ 'UserIDS': newID }, limit = 1) != 0:   # do something 

Used with the optional parameter limit, this provides a way to find if there is at least one matching occurrence.

Limiting the number of matching occurrences makes the collection scan stop as soon as a match is found instead of going through the whole collection.


Note that this can also be written as follow since 1 is interpreted as True in a python condition:

if db.collection.count_documents({ 'UserIDS': newID }, limit = 1):   # do something 

In earlier versions of Mongo/Pymongo, count could be used (deprecated and replaced by count_documents in Mongo 4):

if db.collection.count({ 'UserIDS': newID }, limit = 1) != 0:   # do something 
like image 28
Xavier Guihot Avatar answered Sep 17 '22 10:09

Xavier Guihot