Key-Document Database in MongoDB
Intro
- Strucutre of MongoDB is JOSN format alike.
- No data schema defined in advance. Doucments can differ with number or type of fileds.
- Installing Custer in MongoDB Atlas which is cloud database.
- MongoDB database hierarchy:
1. Database.
2. Collection (analogous to table in a relational db).
3. Document (analogous to row in a relational db). - Distributed system on mutliple servers with cloud solution.
- Fault tolerance - keeping redundant copies of the same data on different servers.
Features
App includes following features:
Demo
MongoDB Atlas:
- Making use of AWS cloud database provider.
- We ca keep our database in the cloud with MongoDB Atlas.
- In MongoDB Atlas we can generate connection code.
- Document can be embedded in another document.
- In above example, the document clients is embedded in one of documents in the products collection.
Python app:
- Getting connected witn MongoDB Atlas using MongoEngine library in Python.
- MongoEngine provides Object Document Mapper (ODM).
- ODM allows application to interact with Python classes that represent database collections.
- With ODM we don't query collections directly. We work with classes defined in Python script.
- class name Products suits collection name products. - Creating instacnce of Products class and excecuting save method on it, saves document into database:
- product = Products(name="TV", price=6999.99, clients=[client1,client2])
- product.save()
Setup
Python libraries installation required:
- pip install mongoengine
Setting up cloud database:
- procced with https://www.mongodb.com/cloud/atlas