Create/alter/drop table(collection) in MongoDB

Manage the table collection in MongoDB

Create table(collection) in MongoDB

-- For Create table in MongoDB:

db.employees.insert( {
emp_id: "RAM",
age: 50,
status: "A"
} )

Or

db.createCollection("employees");

Example of Create Table in SQL in RDBMS databases:

CREATE TABLE employees( id MEDIUMINT NOT NULL  AUTO_INCREMENT,
emp_id Varchar(30),
age Number,
status char(1),
PRIMARY KEY (id) )

Add column in collection of MongoDB

-- Add the join_date column in employees collection.
db.employees.update(
{ },
{ $set: { join_date: "" } },
{ multi: true }
)

Example show add column in SQL RDBMS database:

ALTER TABLE employees ADD join_date DATETIME

Remove column in MongoDB:

-- Remove the join_date column from employees table
db.employees.update(
{ },
{ $unset: { join_date: "" } },
{ multi: true }
)

Example Remove column in SQL RDBMS:

alter table employees drop column join_date;

Drop collection in Mongdb:

-- Drop table employees.
db.employees.drop();

Example for RDBMS database:

drop table users:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.