MongoDB has also developed a GUI tool for handling MongoDB databases. It is called MongoDB Compass. It is a convenient tool for performing all CRUD operations without manually writing queries. It helps in many activities such as indexing, document validation, etc.
Download community edition of MongoDB Compass from https://www.mongodb.com/download-center/compass and start MongoDBCompassCommunity.exe (Ensure that MongoDB server is running before starting Compass). Connect to the local server by giving correct host and port number.
All the databases currently available will be listed as below −
Click on + button (shown at the bottom of left panel) to create new database.
Choose name of database from list and select a Collection as shown below −
You can add document directly or import from CSV or JSON file.
Choose Insert Document from Add data drop down.
Documents added will be displayed in JSON, list or tabular form −
Note that, just as a table in relational database has a primary key, document in MongoDB database has a special key called "_id" that is automatically generated.
MongoDB Inc. provides a Python driver for connection with MongoDB databases. It is called PyMongo whose usage is similar to standard SQL queries.
After installing PyMongo module, we need object of MongoClient class for interacting with MongoDB server.
New database is created with the following statement −
import pymongo myclient = pymongo.MongoClient('mongodb://localhost:27017/') mydb = myclient['mydatabase']
CRUD operations on this database are performed with methods such as insert_one() (or insert_many()), find(), update() and delete() methods. Detailed discussion of PyMongo library is available at https://prutor.ai/python_data_access/python_mongodb_introduction.htm.
However, Python’s user defined objects cannot be stored in database unless it is converted in MongoDB’s data types. This is where we need MongoEngine library.