Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Full Stack FastAPI, React, and MongoDB
Full Stack FastAPI, React, and MongoDB

Full Stack FastAPI, React, and MongoDB: Fast-paced web app development with the FARM stack , Second Edition

Arrow left icon
Profile Icon MongoDB Inc. Profile Icon Marko Aleksendrić Profile Icon Shrey Batra Profile Icon Shubham Ranjan Profile Icon Rachelle Palmer +1 more Show less
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
Paperback Aug 2024 312 pages 2nd Edition
eBook
€44.99
Paperback
€56.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon MongoDB Inc. Profile Icon Marko Aleksendrić Profile Icon Shrey Batra Profile Icon Shubham Ranjan Profile Icon Rachelle Palmer +1 more Show less
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
Paperback Aug 2024 312 pages 2nd Edition
eBook
€44.99
Paperback
€56.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€44.99
Paperback
€56.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Full Stack FastAPI, React, and MongoDB

Setting Up the Database with MongoDB

In this chapter, you will explore some of the main features of MongoDB through several simple yet illustrative examples. You will learn about the basic commands of the MongoDB Query API to start interacting with your data stored in a MongoDB database. You will learn the essential commands and methods that will enable you to insert, manage, query, and update your data.

The aim of this chapter is to help you understand how easy it is to set up a MongoDB database on your local machine or in the cloud and perform the operations that might be needed in a fast-paced web development process.

Querying, through MongoDB methods and aggregation, is best learned by experimenting with data. This chapter utilizes real-world sample datasets provided by MongoDB Atlas that are loaded into your cloud database. You will learn to execute CRUD and aggregation queries against them.

This chapter will cover the following topics:

  • The structure of a MongoDB...

Technical requirements

For this chapter, you will require MongoDB version 7.0.7 and Windows 11 (and Ubuntu 22.04 LTS).

MongoDB version 7.0 is compatible with the following:

  • Windows 11, Windows Server 2019, or Windows Server 2022 (64-bit versions)
  • Ubuntu 20.04 LTS (Focal) and Ubuntu 22.04 LTS (Jammy) for Linux (64-bit releases)

The following are recommended system configurations:

  • A desktop or laptop with at least 8 GB of RAM.
  • There are no CPU requirements specified as such but make sure it’s modern (a multi-core processor) to ensure efficient performance.

The structure of a MongoDB database

MongoDB is widely regarded as the leading NoSQL database in terms of popularity and usage—its power, ease of use, and versatility make it an excellent choice for large- and small-scale projects. Its scalability and performance enable the data layer of your app to have a very solid foundation.

In the following sections, you will take a deeper look into the basic concepts and building blocks of MongoDB: the document, the collection, and the database. Since this book takes a bottom-up approach, you will start from the very bottom and see an overview of the simplest data structures available in MongoDB and then take it up from there into documents, collections, and so on.

Documents

MongoDB is a document-oriented database. But what does that actually mean?

In MongoDB, documents serve a similar purpose to rows in a traditional relational database. Each document in MongoDB is a data structure that consists of key-value pairs, representing...

Installing MongoDB and related tools

MongoDB is not just a database service provider, but a full-fledged developer data platform that has a set of technologies built around the core database to meet all your data needs and improve your productivity as a developer. Let’s examine the following components that you will be installing or using in the following sections:

  • MongoDB Community Edition: A free version of MongoDB that runs on all major operating systems. It is what you are going to use to play around with data locally.
  • MongoDB Compass: A graphical user interface (GUI) for managing, querying, aggregating, and analyzing MongoDB data in a visual environment. Compass is a mature and useful tool that you’ll be using throughout your initial querying and aggregation explorations.
  • MongoDB Atlas: The database-as-a-service solution from MongoDB. This offering is one of the main reasons MongoDB is a central part of the FARM stack. It is relatively easy to set...

Installing MongoDB and Compass on Windows

In this section, you will learn how to install the latest version of MongoDB Community Edition, which at the time of writing is 7.0. MongoDB Community is only supported on 64-bit versions of Windows on x86_64 architecture. Windows versions supported are Windows 11, Windows Server 2019, and Windows Server 2022. To install MongoDB and Compass, you can refer to the following steps.

Note

We strongly advise you to check the instructions on the MongoDB website (https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-windows/) to ensure you have access to the latest information, as they might slightly change.

  1. To download the installer, head over to the MongoDB Download Center at https://www.mongodb.com/try/download/community, select the Windows version, and click on Download as follows:

Figure 2.1: MongoDB download page

  1. Next, execute it. If a security prompt displays Open Executable File...

Installing the MongoDB Shell (mongosh)

After installing MongoDB Community Server and Compass on your computer, you will next install mongosh, the MongoDB Shell.

Note

For instructions on other operating systems, please visit the MongoDB documentation: https://www.mongodb.com/docs/mongodb-shell/install/.

Here’s how you can do it for Windows:

  1. Navigate to the MongoDB Download Center (https://www.mongodb.com/try/download/shell) and, in the Tools section, select MongoDB Shell.
  2. From the dropdowns, select the Windows version and the msi package and click on Download.

Figure 2.4: Download the MongoDB Shell

  1. Next, locate the msi package on your computer and execute it. If a security prompt asks Open Executable File, select Yes and proceed to the MongoDB setup wizard. The wizard will open the following page. Click on Next:

Figure 2.5: The MongoDB Shell Setup Wizard

  1. In the prompt, select the...

MongoDB Database Tools

The MongoDB Database Tools are a collection of command-line utilities for working with a MongoDB deployment. Some of the common database tools are as follows:

  • mongoimport: Imports content from an extended JSON, CSV, or TSV export file
  • mongoexport: Produces a JSON or CSV export of data stored in a mongod instance
  • mongodump: Creates a binary export of the contents of a mongod database

There are some other tools, such as mongorestore, bsondump, mongostat, mongotop, and mongofiles. The MongoDB Database Tools can be installed with an MSI installer (or downloaded as a ZIP archive).

Note

The msi package can be downloaded from the MongoDB Download Center (https://www.mongodb.com/try/download/database-tools).

After downloading, you can follow the installation instructions provided in the MongoDB documentation (https://www.mongodb.com/docs/database-tools/installation/installation-windows/).

The next section provides a walk-through of...

Installing MongoDB and Compass on Linux: Ubuntu

Linux offers numerous benefits for the development and management of local servers, but most importantly, should you decide that the database-as-a-service of MongoDB isn’t what you want to use anymore, you will probably want to work on a Linux-based server.

In this book, we will go over the installation process on Ubuntu version 22.04 LTS (Jammy), while the MongoDB version also supports Ubuntu 20.04 LTS (Focal) on x86_64 architecture. The necessary steps to install MongoDB Ubuntu will be listed here, but you should always check the MongoDB Ubuntu installation page (https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/) for recent changes. The process, however, shouldn’t change.

The following actions are to be performed in a Bash shell. Download the public key that will allow you to install MongoDB, then you will create a list file and reload the package manager. Similar steps are required for other...

Setting up Atlas

MongoDB Atlas—a cloud database service provided by MongoDB—is one of the strongest selling points of MongoDB.

MongoDB Atlas is a fully managed database service, which means that MongoDB handles the infrastructure management, database setup, configuration, and maintenance tasks for you. This allows you to focus on developing your applications instead of managing the underlying infrastructure.

Note

The processes of signing up and setting up a MongoDB Atlas instance are well documented at https://www.mongodb.com/docs/atlas/getting-started/.

There are two ways in which you can set up your Atlas account:

  • Atlas UI (website)
  • Atlas CLI (command line)

The Atlas CLI provides a dedicated CLI for MongoDB Atlas, allowing you to manage your Atlas database deployments and Atlas Search directly from your terminal. In this book, you will see how to do it from the UI.

Head over to https://www.mongodb.com/cloud/atlas/register to create...

MongoDB querying and CRUD operations

Let’s see MongoDB in action and experience firsthand the power of the most popular NoSQL database. This section will show you the most essential MongoDB commands, through simple examples. These methods will enable you, as a developer, to take control of your data, create new documents, query documents by using different criteria and conditions, perform simple and more complex aggregations, and output data in various forms.

Although you will be talking to MongoDB through the Python drivers (Motor and PyMongo), it is helpful to learn how to write queries directly at first. You’ll begin by querying the sample_mflix.movies dataset that was imported into your cluster at the time of cluster creation, then you’ll go through the process of creating new data—inserting, updating, and so on.

Let’s first define the two options for executing MongoDB commands, as follows:

  • Compass GUI
  • MongoDB Shell (mongosh...

Querying in MongoDB

This section will show the use of the sample_mflix.movies collection as an example for demonstration. Working with real data with some expected query results helps reinforce the acquired notions and makes understanding the underlying processes easier and more thorough.

The most frequent MongoDB query language commands—and the ones that this chapter will be covering—are as follows:

  • find(): Finds and selects documents matching simple or complex criteria
  • insertOne(): Inserts a new document into the collection
  • insertMany(): Inserts an array of documents into the collection
  • updateOne() and updateMany(): Update one or more documents according to some criteria
  • deleteOne() and deleteMany(): Delete one or more documents from the collection

There are 21,349 documents in the sample_mflix.movies collection. To query for all the documents, type the following command in the MongoDB Shell:

db.movies.find()

The preceding command...

Summary

This chapter detailed the basic building blocks that define MongoDB and its structure. You have seen how to set up a database in the cloud using MongoDB Atlas and explored the basics of creating, updating, and deleting documents. Further, this chapter detailed the aggregation pipeline framework—a strong analytic tool.

The next chapter will show how to create APIs with FastAPI—an exciting and new Python framework. We will provide a minimal yet complete guide of the main concepts and features, which should hopefully convince you that building APIs can be fast, efficient, and fun.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn the basics of MongoDB to be able to model any type of data
  • Explore the powerful Python web development ecosystem with Pydantic and FastAPI
  • Future-proof your applications by integrating ChatGPT or other LLMs
  • Purchase of the print or Kindle book includes a free PDF eBook

Description

Discover how to harness the power of the FARM stack—FastAPI, React, and MongoDB—to develop production-ready web applications of varying complexity. Written by a team of industry experts, including MongoDB champions and product leaders, this fast-paced, hands-on guide equips beginners with essential skills to build web applications efficiently. Introducing each element of the stack, the book demonstrates how to seamlessly integrate them to create a medium-sized web application. You'll set up MongoDB as a document store, construct a simple API with FastAPI, and build an application using React. The guide also covers enhancing application security through authentication and authorization with JSON Web Tokens. Beyond mastering the stack, you'll get to grips with integrating Large Language Models (like ChatGPT) for advanced functionality, such as automated email sending. Additionally, you'll learn how to make the most of Next.js 14, a robust full-stack framework offering improved developer experience. By the end of the book, you'll have created functional applications and gained the foundation to explore diverse and more specialized domains, expanding your development horizons.

Who is this book for?

The book is for intermediate web developers with basic JavaScript and Python knowledge who want to enhance their developer skills, master a powerful and flexible stack, and write better applications faster.

What you will learn

  • Set up and manage MongoDB databases and collections, and model data
  • Leverage the power of FastAPI to write complex APIs using pure Python
  • Create frontends of varying complexity with the React library
  • Build and deploy robust and secure backends using FastAPI and MongoDB
  • Ship production-ready applications with Next.js 14, applying your React and backend knowledge
  • Integrate Large Language Models, send emails, and do much more with the flexible FARM stack

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 23, 2024
Length: 312 pages
Edition : 2nd
Language : English
ISBN-13 : 9781835886762
Vendor :
MongoDB
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Aug 23, 2024
Length: 312 pages
Edition : 2nd
Language : English
ISBN-13 : 9781835886762
Vendor :
MongoDB
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 136.97 147.97 11.00 saved
Building AI Intensive Python Applications
€56.99
Python Feature Engineering Cookbook
€22.99 €33.99
Full Stack FastAPI, React, and MongoDB
€56.99
Total 136.97 147.97 11.00 saved Stars icon

Table of Contents

13 Chapters
Chapter 1: Web Development and the FARM Stack Chevron down icon Chevron up icon
Chapter 2: Setting Up the Database with MongoDB Chevron down icon Chevron up icon
Chapter 3: Python Type Hints and Pydantic Chevron down icon Chevron up icon
Chapter 4: Getting Started with FastAPI Chevron down icon Chevron up icon
Chapter 5: Setting Up a React Workflow Chevron down icon Chevron up icon
Chapter 6: Authentication and Authorization Chevron down icon Chevron up icon
Chapter 7: Building a Backend with FastAPI Chevron down icon Chevron up icon
Chapter 8: Building the Frontend of the Application Chevron down icon Chevron up icon
Chapter 9: Third-Party Services Integration with FastAPI and Beanie Chevron down icon Chevron up icon
Chapter 10: Web Development with Next.js 14 Chevron down icon Chevron up icon
Chapter 11: Useful Resources and Project Ideas Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(1 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Emiru Aug 31, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
i have just started this book stumbled on it during a React Js class and was hesitant at first. I was blown away by the first few paragraphs and how the writter takes the time to explain why each of the technologies. I have just installed MongoDB and i can see this book being my companion for many years to come. Thank you.
Subscriber review Packt
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.