You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MongoDB is an open-source, document-oriented NoSQL database that was designed for scalability and high availability. MongoDB stores data in JSON-like documents, which makes it easy for developers to work with data because it does not require a fixed schema. This means you can store data without having to define a strict structure, which is different from traditional SQL databases.
16
+
17
+
MongoDB supports CRUD (Create, Read, Update, Delete) operations, ad-hoc queries, and indexing, making it an excellent choice for modern web and mobile applications. It is widely used across various industries because of its flexibility, scalability, and fast performance.
18
+
19
+
### Why is different compared with relational DBMS like MySql, PostgreSQL
20
+
- Data model: Relational databases use a tabular data model, where data is organized into tables with columns and rows. MongoDB uses a document-based data model, where data is stored as documents that can have varying structures and are typically organized in collections.
21
+
22
+
- Storage architecture: Relational databases use a fixed schema to enforce data consistency and use ACID transactions to ensure data integrity. MongoDB uses a flexible schema that allows for faster development and easier data evolution.
23
+
24
+
- Query language: Relational databases use SQL (Structured Query Language) to query data, which is a declarative language that allows users to specify what data they want to retrieve. MongoDB uses a query language called the MongoDB Query Language (MQL), which is a JSON-based language that allows for complex queries of document-based data.
25
+
26
+
- Use cases: Relational databases are well-suited for transactional systems, such as online banking or e-commerce applications, where data consistency and integrity are critical. MongoDB is better suited for use cases where data is less structured and more varied, such as social media or content management systems, where the flexibility of the document model allows for faster development and easier data evolution.
27
+
28
+
### Install MongoDB
29
+
For this tutorial, you need MongoDB installed on your device to get started. To install MongoDB follow the links below and follow the steps outlined to get it done.
Alternatively use [https://www.mongodb.com/try/download/community](https://www.mongodb.com/try/download/community), under `MongoDB Community Server Download`, using the dropdown menu select your platform and installation package. This provides a graphical user interface for the installation.
34
+
35
+
- On Windows, tick the box `Install MongoDB as a Service`, this ensures MongoB is started automatically.
36
+
- On Linux, after installation is complete, run the command `sudo service mongod start` or `sudo systemctl start mongod
37
+
` to start up the MongoDB server.
38
+
39
+
MongoDB shell (`Mongosh`) is used to interact with the MongoDB server from the terminal. For Linux, it is installed with the MongoDB server.
40
+
For Windows `mongosh` is not installed with the MongoDB server, follow the link below to install `mongosh`
* Under `MongoDB Shell Download` using the dropdown menu, select the preferred platform and installation package.
44
+
* After the download is complete, double-click the file and follow the instructions to install the MongoDB shell
45
+
46
+
To use `mongosh` with Windows, you need to add it to PATH. follow the instructions [here](https://www.mongodb.com/docs/mongodb-shell/install/#add-the-mongosh-binary-to-your-path-environment-variable) to add `mongosh` to PATH.
47
+
48
+
Once installation is complete, run the command below to ensure `mongosh` was installed correctly.
49
+
```bash
50
+
$ mongosh
51
+
...
52
+
test> // mongosh cli
53
+
test>exit() // exits mongosh
54
+
```
55
+
This shows that `mongosh` has successfully connected to the MongoDB server. Now you are ready to start development with MongoDB installed on your device.
56
+
57
+
## Setting up a basic Django project
58
+
- From the terminal run the following commands to create a new folder, change the directory into the new folder and create a new virtual environment
59
+
```bash
60
+
$ mkdir django-mongo-integration
61
+
django-mongo-integration$ cd django-mongo-integration
62
+
django-mongo-integration$ virtualenv venv
63
+
```
64
+
65
+
- The command below activates the virtual environment
* Now that the virtual environment is activated, we can start installing the packages needed. Execute the following commands on your terminal to install Django and create a new Django project.
The project is created in the current directory because of `.` added to the command.
83
+
84
+
## Add dependencies for MongoDB
85
+
Django was built to integrate seamlessly with Relational Databases. To use MongoDB with Django and still leverage features like the database modeling, admin panel and so on, we will be needing a package called `Djongo`. The proceeding steps will help us install and configure our Django project to use `Djongo`.
After completing the prompt, you would have created a superuser. Now you can start the application and visit the admin panel using the superuser details entered earlier.
* When the Django server starts running, open your browser and visit `http://127.0.0.1:8000/admin`. This is the route to the admin section. Once you log in you will see the admin panel, which lets you perform several actions, including adding users, deleting users, and assigning and removing roles.
This command will return a list of all the users registered. For now, we have only created the superuser, hence we have only one record in the database.
174
+
175
+
## Style the project using Soft Admin Theme
176
+
Now we have set up our Django project and also connected it to MongoDB using `djongo`. Now we will head on to adding a prebuilt theme to our Django project. The theme to be added is the [`Django admin soft dashboard`](https://github.com/app-generator/django-admin-soft-dashboard). The proceeding steps will guide you in integrating this theme with your Django project.
* After installation, we need to make changes to our project files to allow the project to recognize and use the theme. Open the file `core/settings.py` and make the following changes.
184
+
```py
185
+
# core/settings.py
186
+
...
187
+
INSTALLED_APPS= [
188
+
'admin_soft.apps.AdminSoftDashboardConfig', # <-- New
* Next step is to add a route that points to the theme. Open `core/urls.py and make the following change
198
+
```py
199
+
# core/urls.py
200
+
...
201
+
from django.urls import path, include
202
+
203
+
urlpatterns = [
204
+
path('admin/', admin.site.urls),
205
+
path('', include('admin_soft.urls')), # <-- New
206
+
]
207
+
```
208
+
209
+
Now that all these changes have been made, open your browser and head to `http://127.0.0.1:8000/` to see the changes made. You can also check the admin dashboard by visiting `http://127.0.0.1:8000/admin`. Now we have a working Django project that is connected to MongoDB.

216
+
27
217
- Conclusions
28
-
- Resources Section
218
+
Congratulations on completing the tutorial, so far we have covered the basics of setting up a Django project, connecting Django with MongoDB, viewing data stored in our MongoDB database and also using a prebuilt theme to get a head start in developing a Django application. To sum up, connecting Django to MongoDB is a relatively straightforward process that involves only a few steps. With the power of Django's web framework and MongoDB's scalability and high availability, your applications will be able to handle large volumes of data with ease. By following the above steps, you'll be able to connect Django to MongoDB quickly and begin building powerful and scalable web applications. You can visit [Appseed](https://appseed.us/) for more templates like the one used for this tutorial.
0 commit comments