# How to Create a Table/Collections in MongoDB

> URL: https://www.atlantic.net/dedicated-server-hosting/how-to-create-a-table-collections-in-mongodb/ | Published: 2024-08-06 | Updated: 2024-08-17 | Author: Hitesh Jethva

Before diving into creating tables, it’s essential to understand what a collection is in MongoDB. A collection is a group of MongoDB documents, similar to a table in a relational database. Documents within a collection can have different fields, but typically, they share a similar purpose or structure. Collections do not enforce a schema, meaning each document can contain different fields and data types.

In this tutorial, we’ll learn how to create a table in MongoDB.

## Step 1 – Connecting to MongoDB

First, we need to connect to the MongoDB server. Open your terminal or command prompt and start the MongoDB shell by typing:

```bash
mongo
```

You should see an output indicating that you are connected to the MongoDB server.

```bash
>
```

## Step 2 – Creating a Database

Before creating a collection, we need a database. In MongoDB, databases are created dynamically. You can switch to a new or existing database using the use command. For this tutorial, we’ll create a new database called tutorialDB.

```bash
use tutorialDB
```

The output will be:

```bash
switched to db tutorialDB
```

## Step 3 – Creating a Collection

In MongoDB, collections are created implicitly when you insert data into them. However, you can also create a collection explicitly using the createCollection command.

Let’s create a collection named users by inserting a document into it:

```bash
db.users.insertOne({
    name: "John Doe",
    email: "john.doe@example.com",
    age: 29
})
```

This command creates a collection named users and inserts a document with the specified fields.

```bash
{
	"acknowledged" : true,
	"insertedId" : ObjectId("669514de0bc92b8bfdbe553d")
}
```

Alternatively, you can explicitly create a collection using the createCollection command:

```bash
db.createCollection("products")
```

This command creates an empty collection named products.

```bash
{ "ok" : 1 }
```

## Step 4 – Inserting Documents into a Collection

Now that we have our collections, let’s insert more documents into them.

To insert multiple documents into the users collection, use the insertMany command:

```bash
db.users.insertMany([
    { name: "Alice Smith", email: "alice.smith@example.com", age: 24 },
    { name: "Bob Johnson", email: "bob.johnson@example.com", age: 35 },
    { name: "Charlie Brown", email: "charlie.brown@example.com", age: 28 }
])
```

Output.

```bash
{
	"acknowledged" : true,
	"insertedIds" : [
		ObjectId("669515120bc92b8bfdbe553e"),
		ObjectId("669515120bc92b8bfdbe553f"),
		ObjectId("669515120bc92b8bfdbe5540")
	]
}
```

To insert a single document into the products collection, use the insertOne command:

```bash
db.products.insertOne({
    productName: "Laptop",
    brand: "BrandX",
    price: 799
})
```

The output will be:

```bash
{
    "acknowledged" : true,
    "insertedId" : ObjectId("669515310bc92b8bfdbe5541")
}
```

## Step 5 – Querying Collections

Once we have data in our collections, we can query it. Here are a few basic queries to get you started.

To find all documents in a collection, use the find command:

```bash
db.users.find().pretty()
```

Output:

```bash
{
	"_id" : ObjectId("669514de0bc92b8bfdbe553d"),
	"name" : "John Doe",
	"email" : "john.doe@example.com",
	"age" : 29
}
{
	"_id" : ObjectId("669515120bc92b8bfdbe553e"),
	"name" : "Alice Smith",
	"email" : "alice.smith@example.com",
	"age" : 24
}
{
	"_id" : ObjectId("669515120bc92b8bfdbe553f"),
	"name" : "Bob Johnson",
	"email" : "bob.johnson@example.com",
	"age" : 35
}
{
	"_id" : ObjectId("669515120bc92b8bfdbe5540"),
	"name" : "Charlie Brown",
	"email" : "charlie.brown@example.com",
	"age" : 28
}
```

The pretty method formats the output for readability.

To find specific documents, pass a query document to the find command. For example, to find users who are 29 years old:

```bash
db.users.find({ age: 29 }).pretty()
```

The output will be:

```bash
{
	"_id" : ObjectId("669514de0bc92b8bfdbe553d"),
	"name" : "John Doe",
	"email" : "john.doe@example.com",
	"age" : 29
}
```

## Step 6 – Updating Documents

Updating documents in MongoDB is straightforward. Let’s see how to update a document’s field.

To update a single document, use the updateOne command. For example, to update John Doe’s age:

```bash
db.users.updateOne(
    { name: "John Doe" },
    { $set: { age: 30 } }
)
```

To update multiple documents, use the updateMany command. For example, to set the verified field to true for all users:

```bash
db.users.updateMany(
    {},
    { $set: { verified: true } }
)
```

## Step 7 – Deleting Documents

You can also delete documents from a collection.

To delete a single document, use the deleteOne command. For example, to delete Bob Johnson’s document:

```bash
db.users.deleteOne({ name: "Bob Johnson" })
```

To delete multiple documents, use the deleteMany command. For example, to delete all users who are younger than 30:

```bash
db.users.deleteMany({ age: { $lt: 30 } })
```

## Conclusion

In this article, we covered the basics of creating and managing collections (tables) in MongoDB. We learned how to connect to MongoDB, create a database, create collections implicitly and explicitly, insert documents, query data, update documents, and delete documents. Try to create a MongoDB table on [dedicated server hosting](https://www.atlantic.net/dedicated-server-hosting/) from Atlantic.Net!
