0

This is the version I used :

pymongo 3.9.0, 
mongoengine-0.18.2,
python 3.7.1

I have a simple Document like below:

class MyModel(Document):
    meta = {"collection": "my_model"}

    name = StringField()
    email = StringField()
    address = StringField() 

I want to check whether a record is exists by using the name,email or address .I follow this docs

Therefore I run this query:

user = MyModel.objects(Q(name="myname") | Q(email="abc@mail.com") | Q(address="1234567"))

if user.count() > 0: 
   print "User Existed"

But the result is always return true,which is User already existed,even the collection is empty one.

I also tried if user is None,but the result is still the same.I think is because it always will return a object whether have result or not.

So my question is, how can I check whether a query is returning result or empty result?

ken
  • 2,426
  • 5
  • 43
  • 98
  • it's `user.objects.count()` – Alex Blex Dec 16 '19 at 11:27
  • can u provide an example,cause I get this error `AttributeError: 'QuerySet' object has no attribute 'objects'` – ken Dec 16 '19 at 11:46
  • Sorry, you're right. I misread the question. Your query looks good and does work as you intend. Just checked it with mongoengine-0.18.2 pymongo-3.10.0 Python 3.7.5 – Alex Blex Dec 16 '19 at 12:01
  • you mean my query only work with `mongoengine-0.18.2 pymongo-3.10.0 Python 3.7.5` ? – ken Dec 16 '19 at 12:05
  • I mean it is the version I had installed and was using it to make a quick test. I did not check other versions. I would suggest to add versions you are using to the question. It may help others to answer. Evidently it is not reproducible on all version. – Alex Blex Dec 16 '19 at 12:08
  • pymongo 3.9.0, mongoengine-0.18.2,python 3.7.1 this is what i installed – ken Dec 16 '19 at 12:16
  • ok I understand now – ken Dec 16 '19 at 12:16

0 Answers0