I want to filter data with category value, but I don't know how to do this in mongoengine.
I try:
data = Data.objects.exclude(category="").order_by('-datetime')[:500]
But got error:
exclude() got an unexpected keyword argument 'category'
I didn't find the method in document, how do you do this?
I have the feeling you're mixing up two concepts : filtering documents, and returning document subsets.
exclude tells mongo to return selected documents without the category field. It won't filter the documents, just return the same documents, but without the category field: document subsets. Stripping out what you don't need improves performance. exclude and only achieve the same goal, except exclude blacklists fields, while only whitelists fields.
What you want is to filter documents using a Queryset:
data = Data.objects({'category':{'$ne':''}}).order_by('-datetime')[:500]
or using the double underscore syntax:
data = Data.objects(category__ne='').order_by('-datetime')[:500]
See the tutorial.
The .exclude() method takes only positional arguments; fields name. That being you need to use a query operator to filter out document where category is empty string using category__ne=''
data = Data.objects(category__ne='').order_by('-datetime')[:500]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With