Skip to content
Myr

mongoengine.rst

This file

Path

docs/patterns/mongoengine.rst

102 lines · 2.8 KiB

Last changed by

Merge branch 'stable'David Lordbefore adoption9fcd34c

History of this file

Take it

1MongoDB with MongoEngine2========================34Using a document database like MongoDB is a common alternative to5relational SQL databases. This pattern shows how to use6`MongoEngine`_, a document mapper library, to integrate with MongoDB.78A running MongoDB server and `Flask-MongoEngine`_ are required. ::910    pip install flask-mongoengine1112.. _MongoEngine: http://mongoengine.org13.. _Flask-MongoEngine: https://docs.mongoengine.org/projects/flask-mongoengine/en/latest/1415Configuration16-------------1718Basic setup can be done by defining ``MONGODB_SETTINGS`` on19``app.config`` and creating a ``MongoEngine`` instance. ::2021    from flask import Flask22    from flask_mongoengine import MongoEngine2324    app = Flask(__name__)25    app.config['MONGODB_SETTINGS'] = {26        "db": "myapp",27    }28    db = MongoEngine(app)293031Mapping Documents32-----------------3334To declare a model that represents a Mongo document, create a class that35inherits from ``Document`` and declare each of the fields. ::3637    import mongoengine as me3839    class Movie(me.Document):40        title = me.StringField(required=True)41        year = me.IntField()42        rated = me.StringField()43        director = me.StringField()44        actors = me.ListField()4546If the document has nested fields, use ``EmbeddedDocument`` to47defined the fields of the embedded document and48``EmbeddedDocumentField`` to declare it on the parent document. ::4950    class Imdb(me.EmbeddedDocument):51        imdb_id = me.StringField()52        rating = me.DecimalField()53        votes = me.IntField()5455    class Movie(me.Document):56        ...57        imdb = me.EmbeddedDocumentField(Imdb)585960Creating Data61-------------6263Instantiate your document class with keyword arguments for the fields.64You can also assign values to the field attributes after instantiation.65Then call ``doc.save()``. ::6667    bttf = Movie(title="Back To The Future", year=1985)68    bttf.actors = [69        "Michael J. Fox",70        "Christopher Lloyd"71    ]72    bttf.imdb = Imdb(imdb_id="tt0088763", rating=8.5)73    bttf.save()747576Queries77-------7879Use the class ``objects`` attribute to make queries. A keyword argument80looks for an equal value on the field. ::8182    bttf = Movie.objects(title="Back To The Future").get_or_404()8384Query operators may be used by concatenating them with the field name85using a double-underscore. ``objects``, and queries returned by86calling it, are iterable. ::8788    some_theron_movie = Movie.objects(actors__in=["Charlize Theron"]).first()8990    for recents in Movie.objects(year__gte=2017):91        print(recents.title)929394Documentation95-------------9697There are many more ways to define and query documents with MongoEngine.98For more information, check out the `official documentation99<MongoEngine_>`_.100101Flask-MongoEngine adds helpful utilities on top of MongoEngine. Check102out their `documentation <Flask-MongoEngine_>`_ as well.