Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Create a database in MongoDB using Python
Next article icon

MongoDB is a general-purpose, document-based, distributed database built for modern application developers and the cloud. It is a document database, which means it stores data in JSON-like documents. This is an efficient way to think about data and is more expressive and powerful than the traditional table model. MongoDB has no separate command to create a database. Instead, it uses the use command to create a database. The use command is used to switch to the specific database. If the database name specified after the use keyword does not exist, then a new database is created with the specified name.

Creating a database using Python in MongoDB

To use Python in MongoDB, we are going to import PyMongo. From that, MongoClient can be imported which is used to create a client to the database. Using the client, a new database can be created.

Example:List of databases using MongoDB shell (before)python-create-database-mongodb1

Python
frompymongoimportMongoClientclient=MongoClient('localhost',27017)db=client['GFG']print("Database is created !!")

Output

Database is created!!

In the above example, it is clearly shown how a database is created. When creating a client, the local host along with its port number, which is 27017 here, is passed to the MongoClient. Then, by using the client, a new database named 'GFG' is created. We can check if the database is present in the list of databases using the following code: 

Python
list_of_db=client.list_database_names()if"mydbase"inlist_of_db:print("Exists !!")

Output

Exists!!

List of Databases in MongoDB shell (after):python-mongodvb-create-database-2


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp