My app code is based on MongoEngine, but how can I access the raw PyMongo APIs when MongoEngine is not able to meet my needs?
Internally, MongoEngine use PyMongo client to access the MongoDB server.
In a MongoEngine app, we can call a Document class's _get_collection
method to get the PyMongo collection object with which we can easily call PyMongo APIs.
import mongoengine as me
class User(me.Document):
name = me.StringField()
me.connect('test')
coll = User._get_collection()
print(coll.find_one())
In the code above, User._get_collection()
returns the PyMongo collection object. We can call PyMongo APIs like find_one()
with this collection object.