Category Archives: MongoDB

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: