Exploring MongoDB and Its Primitives
MongoDB is a popular NoSQL database that operates on the principle of collections and documents contrary to rows and columns in relational databases. It is important to note that MongoDB shines in scenarios dealing with unstructured data, especially when data shapes evolve over time. This makes MongoDB an ideal candidate for big data storage, real-time analytics, mobile applications, content management systems, IoT applications, and more.
Collection in MongoDB can be perceived as a table from the relational database model whereas documents can be regarded as a tuple or row. However, a significant difference is the absence of schema enabling more flexibility in data representation.
Let's now dive into some Python code to interact with MongoDB. We're going to connect to a MongoDB data store, create a collection (table), insert a document (row), and retrieve the inserted document. (Please ensure MongoDB is up and running at port 27017 on localhost in your local system).
In the subsequent lessons, we will be developing a basic version of MongoDB as part of our 'Build Datastores From Scratch' course. This fundamental learning will aid us in understanding how such robust technologies operate and how they can serve industries like finance and AI.
xxxxxxxxxx
from pymongo import MongoClient
if __name__ == '__main__':
# Python logic here
# Establishing connection
client = MongoClient('mongodb://localhost:27017/')
# Creating a database
db = client['sample_db']
# Creating a collection (table)
collection = db['person']
# Inserting a document (row)
collection.insert_one({'name': 'Alice', 'age': 25, 'interests': ['finance', 'AI']})
# Fetching the document
print(collection.find_one({'name': 'Alice'}))
print('Inserted and fetched document successfully.')