question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] How I can use FastAPI with MongoDB libraries?

See original GitHub issue

First check

  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.

Description

How can I use MongoDB using libraries such a pymongo or MongoEngine? I tried to create a basic project with pymongo, but when I try to return MongoDB entity using .find_one() for example, I got an error that ... ObjectId does not iterable.... Then I tried MongoEngine, but when I return for example Article.objects, it just returns "_cls": "".

Is it possible to use MongoDB in FastAPI or I can use only SQLAlchemy?

Additional context

I also found similar issues in a repo and some answers on StackOverflow but doesn’t found how to return MongoDB entities in fastapi responses.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
chris-allnuttcommented, Apr 25, 2020

Have you tried using the json utils described above https://api.mongodb.com/python/current/api/bson/json_util.html I truly think that’s the solution to your problem. You need a serializer that understands mongo objects, that serializer does.

1reaction
tiangolocommented, Jun 6, 2020

Thanks for the help here everyone! 👏 🙇

I assume you solved your problem, so thanks for reporting back and closing the issue @KirillSaltykov 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use FastAPI with MongoDB - Python in Plain English
Integrate FastAPI with MongoDB is easier than it looks. Here you're going to see ... FastAPI uses the Pydantic library to check the...
Read more >
Building a CRUD App with FastAPI and MongoDB
In this tutorial, you'll learn how to develop an asynchronous API with FastAPI and MongoDB. We'll be using the Motor package to interact...
Read more >
Getting Started with MongoDB and FastAPI
MongoDB stores data as BSON. FastAPI encodes and decodes data as JSON strings. BSON has support for additional non-JSON-native data types, ...
Read more >
69. FastAPI and MongoDB - YouTube
FastAPI Authentication Example With OAuth2, JSON Web Tokens and Tortoise ORM · FAST and MODERN Python API with FastAPI | FastAPI tutorial.
Read more >
Benchmarking FastAPI and MongoDB Options - Medium
When it comes to Python, MongoDB and FastAPI, you have two main options: PyMongo, the official Python driver for MongoDB, or Motor, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found