- MongoEngine - Home
- MongoEngine - MongoDB
- MongoEngine - MongoDB Compass
- MongoEngine - Object Document Mapper
- MongoEngine - Installation
- MongoEngine - Connecting to MongoDB Database
- MongoEngine - Document Class
- MongoEngine - Dynamic Schema
- MongoEngine - Fields
- MongoEngine - Add/Delete Document
- MongoEngine - Querying Database
- MongoEngine - Filters
- MongoEngine - Query Operators
- MongoEngine - QuerySet Methods
- MongoEngine - Sorting
- MongoEngine - Custom Query Sets
- MongoEngine - Indexes
- MongoEngine - Aggregation
- MongoEngine - Advanced Queries
- MongoEngine - Document Inheritance
- MongoEngine - Atomic Updates
- MongoEngine - Javascript
- MongoEngine - GridFS
- MongoEngine - Signals
- MongoEngine - Text Search
- MongoEngine - Extensions
MongoEngine Useful Resources
MongoEngine - Filters
The objects attribute is a QuerySet manager. It creates and returns a QuerySet when accessed. A query can be subjected to filter with the help of field names as keyword arguments. For example, from above products collection, to print details of document with name of product as TV, we use Name as keyword argument.
main.py
from mongoengine import *
con = connect('myDb')
class Product(Document):
productID = IntField(required=True)
name = StringField()
price = IntField()
for product in Product.objects(name='TV'):
print ('ID:',product.productID, 'Name:',product.name, 'Price:',product.price)
Output
Compile and run the above code and verify the output −
ID: 2 Name: TV Price: 50000
You can use filter method of QuerySet object to apply filter to query. Following code snippet also returns product details with name=TV.
main.py
from mongoengine import *
con = connect('myDb')
class Product(Document):
productID = IntField(required=True)
name = StringField()
price = IntField()
qset=Product.objects
for product in qset.filter(name='TV'):
print ('ID:',product.productID, 'Name:',product.name, 'Price:',product.price)
Output
Compile and run the above code and verify the output −
ID: 2 Name: TV Price: 50000
Advertisements