Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoengine check if object exists without fetching the object

Tags:

mongoengine

On Django we can use the QuerySet.exists() to check if an object exists in the DB in the most efficient way, without actually fetching the record.

Is there an equivalent function on Mongoengine?

like image 870
Tzach Avatar asked Mar 05 '17 16:03

Tzach


1 Answers

According to official documentation, here is solution how to make this if you have object id.Its the best solution for your case, that i have seen in mongoengine documentation.It works as below:

# Returns None or Object if it exists
result = Collection.objects.with_id(object_id=*your object id*)
if result is None:
  # raise error
else:
  # make some actions with this object

Is this is what are you looking for?

like image 57
lucas63 Avatar answered Oct 20 '22 05:10

lucas63