Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

329
Vistas
FastAPI refuses to let me create a mongoengine document

I have FastAPI Python application with routes that operate on a MongoDB instance. The connection works fine, and I can query documents for my GET endpoints, but creating a new document from within FastAPI seems impossible.

I consistently get:

You have not defined a default connection

I have a standalone script that handles some data migration tasks and it uses the exact same DB class and Document models that the FastAPI app does, and that script is able to save documents to mongo perfectly fine. There is no difference in how the DB object is instantiated between the API and the script.

The DB class:

from os import getenv

from mongoengine import connect
from pymongo import MongoClient
from pymongo.errors import ServerSelectionTimeoutError

class Mongo:

    @property
    def target_db(self):
        return 'some_db'

    @property
    def uri(self) -> str:
        env_uri = getenv('MONGODB', None)
        if env_uri is None:
            raise DBError('MONGODB environment variable missing')
        return env_uri.strip()

    def connect(self) -> MongoClient:
        try:
            return connect(host=self.uri, db=self.target_db, alias=self.target_db)
        except ServerSelectionTimeoutError as e:
            raise ServerSelectionTimeoutError(e)

All of my DB models have meta attributes defining exactly what DB and collection to use:

class Thing(Document):
    meta = {'db_alias': 'some_db',
            'collection': 'things'}

Queries on existing documents succeed inside of a route definition:

results = Thing.objects.filter(**query)
# This returns things that I can iterate over

Document creation fails inside of a route definition:

new_thing = Thing(**creation_args)
new_thing.save()

Error:

mongoengine.connection.ConnectionFailure: You have not defined a default connection

What does that even mean? I know that I'm connected because I can query the db.

How is it possible that I can successfully query documents from Mongo but not save them? Every suggestion I have seen online points to not having defined a db or alias in the call to mongoengine.connect, but I clearly am in my Mongo object, and even if that were true, surely I wouldn't be able to retrieve documents from the db collection...

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The mongoengine Document models had malformatted meta attributes...

class Accounts(Document):
    meta = {'db_alias': 'some_db',
            'collection': 'things'}

Solution: ​db_alias​ needed to be changed to ​db​.

I didn't find this through documentation, and definitely not through the extremely unhelpful error messages. I just tried it on a whim. Now everything works using the FastAPI framework.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda