Fundamental of MongoDB using Python
About MongoDB :
- MongoDB is a NoSQL database, NoSQL stands for Not Only Structured Query Language
- NoSQL databases like MongoDB is put data in JSON format and SQL databases are stored data in tabular format.
- Data is in JSON format it means it is key values pair because this data retrieval in NoSQL is very very faster as compared to SQL
- Table in Mangodb called as Collection and record is called as document
- It supports geospatial query it means it returns longitude and latitude
- It is easily integrated with Big Data Hadoop
- It is easy to install, scalable, high performance
- For High transaction data Mangobd not good
Installing MongoDB (Community Edition):
- STEP 1: Download the installer file from the download center [Click]
- STEP 2: Run the installer that you have downloaded
- STEP 3: Follow along with the installation wizard
- 1: Choose the setup type Complete
- 2: Select install MongoDB as a Service
- STEP 4: Install MongoDB compass (Optional)
Once MongoDB is installed you’ll see the following screens :
Now, Click on New Connection and copy-paste the connection string “mongodb://localhost:27017/” and click on connect.
Installing Python driver for MongoDB :
The PyMongo distribution contains tools for interacting with the MongoDB database from Python.
Use the following command in your command prompt to install pymongo:
- pip install pymongo
- conda install pymongo
- python -m pip install pymongo
- After installing the driver we need to import it in our python IDE for that use command :
import pymongo
- Make Connection of MongoDB and Python using driver pymongo:
client = pymongo.MongoClient("mongodb://localhost:27017")
- Create a new Database name as a demo
db = client[‘demo’]
- Check a list of databases in MongoDB
client.list_database_names()
- Create a collection
collection_name = 'FIRST_COLLECTION'
collection = db[collection_name]
- Insert a single record in the collection
record = {"name":"John","Education":"BE","Phone":798361894619}db.collection.insert_one(record)
- Find record
collection.find_one()
- Insert multiple records in the collection
multi_record = [
{"name":"Alie","Education":"ME","Phone":761894619},{"name":"Robert","Education":"BE","Phone":8361894619},{"College":"IITB","Education":['BE','ME'],"Phone":794619},{"City":"LONDON","Education":['BE','ME','BSC'],"Phone":798361894619}db.collection.insert_many(multi_record)
- Find all records
collection.find()
I believe this blog assists you to learn :
- what is MongoDB?
- Why we use MongoDB?
- How to install MongoDB?
- How to connect MongoDB with python?
- How to create a Database and Collection in it?
For more knowledge of MongoDB please take a glance at the official documentation.