Skip to content
This repository was archived by the owner on Sep 18, 2024. It is now read-only.

mongodb-developer/django-pymongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Notice: Repository Deprecation

This repository is deprecated and no longer actively maintained. It contains outdated code examples or practices that do not align with current MongoDB best practices. While the repository remains accessible for reference purposes, we strongly discourage its use in production environments. Users should be aware that this repository will not receive any further updates, bug fixes, or security patches. This code may expose you to security vulnerabilities, compatibility issues with current MongoDB versions, and potential performance problems. Any implementation based on this repository is at the user's own risk. For up-to-date resources, please refer to the MongoDB Developer Center.

django-pymongo

To show how we can connect django projects to mongodb using pymongo driver

Django and MongoDB Setup

To get the integration working, you should have a Django and MongoDB setup. If you have Python on your machine, you can install Django using pip. If you wish to install Django in a specific environment, instead of the whole system, you can create a virtual environment. Use pip/pip3 depending on your Python version:

Install:

Windows:

pip install virtualenvwrapper-win

Mac OS / Linux:

pip install virtualenvwrapper

Create:

Windows:

mkvirtualenv MyProjectEnvt

Mac OS / Linux:

virtualenv MyProjectEnvt

Activate:

Mac OS / Linux:

source MyProjectEnvt/bin/activate

Windows:

workon MyProjectEnvt

To deactivate the virtual environment, you can just type the command deactivate

Now install Django using pip install Django To start a Django project, go to the folder where you want to start the project and use django-admin startproject <project_name>.

For example,

C:\Users\myuser\project_files>django-admin startproject MyFirstDjangoProj
C:\Users\myuser\project_files>cd MyFirstDjangoProj

To create an app, use the command,

python manage.py startapp myfirstapp

If you are using the Python version >= 3.0, replace python with python3 in your commands. Inside the app, we can have many models that will be mapped to the collections and documents in MongoDB. Once you start the project, all the files will be available in the project folder. Start the server using the python manage.py runserver command

That’s about our Django setup. If you don’t already have a MongoDB setup, use MongoDB Atlas, to make the most of cloud hosting. Atlas works seamlessly with all the major cloud providers.

Connect Django and MongoDB using PyMongo

PyMongo is the official Python driver for MongoDB. It is very efficient for writing JSON data to MongoDB and you can use MongoDB queries in the Python code itself. We can retrieve data in a dictionary like syntax using PyMongo. Install PyMongo easily using the pip/pip3 command: pip install pymongo[snappy,gssapi,srv,tls]

If you are using a virtual environment (which you are!), you have to install pymongo in ..\venv\Lib\site-packages folder. Also install dnspython for using mongodb+srv:// URIs with the command: pip install dnspython

Using PyMongo, we can concurrently run multiple databases, by specifying the right database name to the connection instance. Let us create a sample pymongo session. There are two approaches for this: We can create a client in the utils file that can be used by any view that wants to interact with MongoDB. Create a utils.py file in your project folder (same location as manage.py) and instantiate the client:

from pymongo import MongoClient
def get_db_handle(db_name, host, port, username, password):

    client = MongoClient(host=host,
                         port=int(port),
                         username=username,
                         password=password
                        )
    db_handle = client['db_name']
    return db_handle, client

This method can then bebe used in ./myfirstapp/view.py. Another approach to get the connection is to use the connection_string:

from pymongo import MongoClient
client = pymongo.MongoClient('connection_string')
db = client['db_name']

where,

connection_string = mongodb+srv://<username>:<password>@<atlas cluster>/<myFirstDatabase>?retryWrites=true&w=majoritymongodb+srv://<username>:<password>@<ip-address>:<port>/<database_name>

For example,

makemytrip_db = client['makemytrip']
#collection object
trips_collection = makemytrip_db['trips']

You may have seen the ‘Connection’ class being used in other code samples or tutorials – Connection has been deprecated, so don’t use it. If you are on a default port and host, simply call MongoClient(). To connect to localhost, we can specify host and port explicitly as: MongoClient(‘localhost’, 27017) or use the URL format MongoClient(‘mongodb://localhost: 27017/’) Since we have created the client here, we need to comment the DATABASES section in settings.py file. Comment the same using triple quotes.

Django and MongoDB Tutorial

In this quick tutorial, we will demonstrate how to use pymongo to do simple CRUD operations. For this, let’s create a pymongo session:

import pymongo
#connect_string = 'mongodb+srv://<username>:<password>@<atlas cluster>/<myFirstDatabase>?retryWrites=true&w=majority' 

from django.conf import settings
my_client = pymongo.MongoClient(connect_string)

#Define the database name
dbname = my_client['sample_medicines']

#Get/create collection name (remember that you will see the database in your mongodb cluster only after you create a collection
collection_name = dbname["medicinedetails"]

#Create two documents
medicine_1 = {
    "medicine_id": "RR000123456",
    "common_name" : "Paracetamol",
    "scientific_name" : "",
    "available" : "Y",
    "category": "fever"
}
medicine_2 = {
    "medicine_id": "RR000342522",
    "common_name" : "Metformin",
    "scientific_name" : "",
    "available" : "Y",
    "category" : "type 2 diabetes"
}

#Insert the documents
collection_name.insert_many([medicine_1,medicine_2])

#Check the count
count = collection_name.count()
print(count)

#Read the documents
med_details = collection_name.find({})

#Print on the terminal
for r in med_details:
    print(r["common_name"])

#Update one document
update_data = collection_name.update_one({'medicine_id':'RR000123456'}, {'$set':{'common_name':'Paracetamol 500'}})

#Delete one document
delete_data = collection_name.delete_one({'medicine_id':'RR000123456'})

Connect to your MongoDB Atlas cluster to see the changes.

Disclaimer

This software is not supported by MongoDB, Inc under any of their commercial support subscriptions or otherwise. Any usage is at your own risk.

About

To show how we can connect django projects to mongodb using pymongo driver

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •