Skip to content
Myr

mongoengine.rst

This file

Path

docs/patterns/mongoengine.rst

103 lines · 2.8 KiB

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than adeaf27 point at it.

Merge pull request #3904 from pallets/update-projectDavid Lordbefore adoptionadeaf27

lines 1–103why, one step back
1MongoDB with MongoEngine
2========================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://flask-mongoengine.readthedocs.io141516Configuration17-------------1819Basic setup can be done by defining ``MONGODB_SETTINGS`` on20``app.config`` and creating a ``MongoEngine`` instance. ::2122 from flask import Flask23 from flask_mongoengine import MongoEngine2425 app = Flask(__name__)26 app.config['MONGODB_SETTINGS'] = {27 "db": "myapp",28 }29 db = MongoEngine(app)303132Mapping Documents33-----------------3435To declare a model that represents a Mongo document, create a class that36inherits from ``Document`` and declare each of the fields. ::3738 import mongoengine as me3940 class Movie(me.Document):41 title = me.StringField(required=True)42 year = me.IntField()43 rated = me.StringField()44 director = me.StringField()45 actors = me.ListField()4647If the document has nested fields, use ``EmbeddedDocument`` to48defined the fields of the embedded document and49``EmbeddedDocumentField`` to declare it on the parent document. ::5051 class Imdb(me.EmbeddedDocument):52 imdb_id = me.StringField()53 rating = me.DecimalField()54 votes = me.IntField()5556 class Movie(me.Document):57 ...58 imdb = me.EmbeddedDocumentField(Imdb)596061Creating Data62-------------6364Instantiate your document class with keyword arguments for the fields.65You can also assign values to the field attributes after instantiation.66Then call ``doc.save()``. ::6768 bttf = Movie(title="Back To The Future", year=1985)69 bttf.actors = [70 "Michael J. Fox",71 "Christopher Lloyd"72 ]73 bttf.imdb = Imdb(imdb_id="tt0088763", rating=8.5)74 bttf.save()757677Queries78-------7980Use the class ``objects`` attribute to make queries. A keyword argument81looks for an equal value on the field. ::8283 bttf = Movies.objects(title="Back To The Future").get_or_404()8485Query operators may be used by concatenating them with the field name86using a double-underscore. ``objects``, and queries returned by87calling it, are iterable. ::8889 some_theron_movie = Movie.objects(actors__in=["Charlize Theron"]).first()9091 for recents in Movie.objects(year__gte=2017):92 print(recents.title)939495Documentation96-------------9798There are many more ways to define and query documents with MongoEngine.99For more information, check out the `official documentation100<MongoEngine_>`_.101102Flask-MongoEngine adds helpful utilities on top of MongoEngine. Check103out their `documentation <Flask-MongoEngine_>`_ as well.