Sling Academy
Home/MongoDB/3 ways to create a new database in MongoDB

3 ways to create a new database in MongoDB

Last updated: February 01, 2024

Introduction

MongoDB, as a leading NoSQL database, offers flexibility in managing and storing data. While defining a database schema upfront isn’t required, you may want to create a new database for organizing data for different applications or services. In this post, we will explore several methods to create a new database in MongoDB.

Using the MongoDB Shell

The MongoDB Shell is a command-line interface that allows you to interact with your MongoDB instance. It’s great for administrative tasks like database creation. The process is straightforward and involves the use of the ‘use’ command to switch to the desired new database. If it doesn’t exist, MongoDB will create it.

  1. Start the MongoDB shell by running mongo in your terminal.
  2. Type use newDatabaseName, replacing ‘newDatabaseName’ with the name of the database you wish to create.
  3. To persist the database, insert at least one document into a collection within it.

Example:

use newDatabase

db.myNewCollection.insert({"key": "value"})

Notes: The database is not fully created until data is inserted. It’s best used for development or administrative purposes rather than from applications directly.

Using a Driver API

Most programming environments support MongoDB through driver libraries. These libraries offer APIs to interact with MongoDB databases programmatically. Similar to the shell, databases are created on-demand when data is stored.

  1. Choose and install the appropriate MongoDB driver for your programming language.
  2. Connect to the MongoDB instance within your application.
  3. Define the new database name and operate on it, usually just by referencing it.
  4. Save data to a collection in that database to persist it.

Example (Node.js):

// JavaScript example using the Node.js MongoDB Driver
const { MongoClient } = require('mongodb')

async function createDatabase() {
  const client = new MongoClient('mongodb://localhost:27017')
  await client.connect()

  const db = client.db('newDatabase')
  const collection = db.collection('myNewCollection')

  await collection.insertOne({"key": "value"})
  console.log('Database created and data inserted.')
  client.close()
}

createDatabase()

Notes: This approach is best integrated into applications. You should ensure that database names are collision-free if auto-generating them.

Using MongoDB Compass

MongoDB Compass is a graphical user interface for MongoDB. It’s user-friendly and intended for those who prefer graphical tools over command-line operations.

  1. Install and open MongoDB Compass. Connect to your MongoDB server.
  2. In the left sidebar, click on the ‘Create Database’ button.
  3. Enter your new database name and the name of the first collection.
  4. Click ‘Create Database’ to finish.

Notes: This process is intuitive and eliminates potential syntax errors from manually typing commands, ideal for beginners or infrequent tasks.

Conclusion

In conclusion, creating a new database in MongoDB can be done in multiple ways to cater to different preferences and use cases, such as using the MongoDB Shell, Driver API, or Compass tool. The MongoDB Shell and driver APIs are suitable for developers who prefer scripting and coding, offering high levels of control and integration with code. On the other hand, MongoDB Compass provides a more visual and user-friendly approach, ideal for those not as comfortable with the command line. All methods meet the same fundamental requirement: a MongoDB database is only created once data is inserted into a collection, emphasizing MongoDB’s flexibility and dynamic nature.

Next Article: How to add comments into MongoDB queries?

Previous Article: How to migrate from MySQL to MongoDB

Series: MongoDB Tutorials

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)