Tuesday, February 21, 2023

Insertion Function

insert_one function can be used to insert one document at a time in MongoDB. We will first create a dictionary and then insert it into the MongoDB database:

post = {"_id": 'qwertyui123456',

        'username': 'someone', 

        'name':'Gyan', 

        'address':'Somewhere in India'}

        

post_id = table.insert_one(post).inserted_id

post_id

Output: qwertyui123456 


MongoDB is an unstructured database so it is not necessary that all the documents in a collection will follow the same structure. For example, the dictionary we inserted in the above case does not contain a few of the fields we have seen in the MongoDB document we fetched earlier.

.inserted_id provides the _id field assigned by default if it has not been provided in the dictionary. In our case, we have explicitly provided this field. Finally, the operation returns the _id of the inserted MongoDB document. It is stored in the post_id variable in the above case. 

So far, we had to insert only one document in the MongoDB collection. What should we do if we have to insert thousands of documents at once? Will you run insert_one in a loop? Not at all!

We have the insert_many function for this:

import datetime

new_posts = [{"name": "Mike",

            "username": "latestpost!",

            "date": datetime.datetime(2009, 11, 12, 11, 14)},

            {"name": "Eliot",

            "title": "onceAgain",

            "text": "and pretty easy too!",

            "date": datetime.datetime(2009, 11, 10, 10, 45)}]


final = table.insert_many(new_posts)

final.inserted_ids



We have imported the datetime library because there is no built-in datatype for date and time in Python. This library will help us to assign the values of datetime type. In the above case, we have inserted a list of dictionaries in the MongoDB database. Each element is inserted as an independent document in the MongoDB collection.

Share:

Friday, February 17, 2023

Retrieving / Fetching the Data

We can query MongoDB using a dictionary-like notation or the dot operator in PyMongo. In the previous section, we used the dot operator to access the MongoDB database. Here, we will also see a demonstration of a dictionary-like syntax.

First, let’s fetch a single document from the MongoDB collection. We’ll use the find_one function for this purpose:

table.find_one()


We can see that the function has returned a dictionary. Let’s see the keys of this dictionary and then I will explain the purpose of each key.

first_instance.keys()


We can see some of the keys are self-explanatory. Let me explain what each of these keys is storing:

  • _id: MongoDB assigns a unique Id to each document
  • username: It contains the username of the user
  • name: The name of the user
  • address: Address of the user is stored in this field
  • birthdate: This argument stores the Date of Birth of the user
  • email: This is the email id of a given user
  • active: This field tells whether the user is active or not
  • accounts: It stores the list of all the accounts held by a given user. A user can have multiple accounts
  • teir_and_details: The category (silver, gold, etc.) is stored in this argument. This field also stores the benefits they are entitled to

Now, let’s see an example of dictionary-like access for MongoDB. Let’s fetch the name of the customer from the MongoDB document:

first_instance['name']

We can also use the find function to fetch the documents. find_one fetches only one document at a time. On the other hand, find can fetch multiple documents from the MongoDB collection:

table.find().sort("_id",pymongo.DESCENDING)

Here, the sort function sorts the documents in the descending order of _id.

Share:

Tuesday, February 14, 2023

PyMongo and MongoDB

PyMongo is a Python library that enables us to connect with MongoDB. It allows us to perform basic operations on the MongoDB database.

So, why Python? It’s a valid question.

We have chosen Python to interact with MongoDB because it is one of the most commonly used and considerably powerful languages for data science. PyMongo allows us to retrieve the data with dictionary-like syntax.

We can also use the dot notation to access MongoDB data. Its easy syntax makes our job a lot easier. Additionally, PyMongo’s rich documentation is always standing there with a helping hand. We will use this library for accessing MongoDB.

MongoDB is available for Linux, Windows and Mac OS X operating systems. Once you have installed the database, you need to start the mongod service. It’s time to fire up your Python notebook and get coding! We have a solid idea of MongoDB – let’s put that knowledge into action.

We will be performing a few key basic operations on a MongoDB database in Python using the PyMongo library.Connecting to the Database

To retrieve the data from a MongoDB database, we will first connect to it. Write and execute the below code in your Jupyter cell in order to connect to MongoDB:

import pymongo 

import pprint

mongo_uri = "mongodb://localhost:27017/"  

client = pymongo.MongoClient(mongo_uri)


Let’s see the available databases:

client.list_database_names()

We will use the sample_analytics database for our purpose. Let’s set the cursor to the same database:

db = client.sample_analytics

The list_collection_names command shows the names of all the available collections:

db.list_collection_names()


Let’s see the number of customers we have. We will connect to the customers collection and then print the number of documents available in that collection:

table=db.customers 

table.count_documents({}) #gives the number of documents in the table

Output: 500

Here, we can see that we have the data for 500 customers. Next, we will fetch a MongoDB document from this table and see what information is present there.




Share:

Saturday, February 11, 2023

Understanding the Problem Statement

Let’s understand the problem we’ll be solving in future. This will give you a good idea of the kind of projects you can pick up to further hone your MongoDB in Python skills.

Suppose you are working for a banking system that provides an application to the customers. This app sends data to your MongoDB database. This data is stored in three collections:

  • The accounts collection contains information about all the accounts
  • The customers collection contains information about a customer
  • Finally, the transactions collection contains the customer transactions data

I have taken the sample database from MongoDB Atlas, a global cloud database service. We will use the ‘sample_analytics’ database to work on this problem statement. This database contains data related to financial services.

Share: