Create a new mongodb database
Here are the steps to create a new MongoDB database:
Method 1: Using the MongoDB Compass GUI
- Download and install MongoDB Compass, a free GUI tool for MongoDB.
- Launch MongoDB Compass and click on "Connect" in the top left corner.
- Enter the connection details:
- Host:
localhost
(or the hostname or IP address of your MongoDB server) - Port:
27017
(the default port for MongoDB) - Authentication:
None
(unless you have authentication set up on your MongoDB server)
- Host:
- Click "Connect" to establish a connection to your MongoDB server.
- In the "Databases" tab, click the "Create Database" button.
- Enter a name for your new database (e.g. "mydatabase") and click "Create".
Method 2: Using the MongoDB Command-Line Tool
-
Open a terminal or command prompt and navigate to the directory where you installed MongoDB.
-
Run the following command to start the MongoDB shell:
mongo
-
In the MongoDB shell, run the following command to create a new database:
use mydatabase
Replace
mydatabase
with the name you want to give your new database. -
If the database doesn't exist, MongoDB will create it. You can verify this by running:
show dbs
This should list all the databases on your MongoDB server, including the new one you just created.
Method 3: Using a programming language (e.g. Python)
- Install the
pymongo
library for Python (or the equivalent library for your programming language of choice). - Import the library and create a connection to your MongoDB server:
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
3. Create a new database using the `create_database` method:
```python
db = client['mydatabase']
Replace mydatabase
with the name you want to give your new database.
- Verify that the database was created by running:
print(db.name)
This should output the name of the new database.
That's it! You now have a new MongoDB database.