Category Archives: MongoDB

Create/alter/drop table(collection) in MongoDB

Create/alter/drop table(collection) in MongoDB

For Create Table in SQL:

CREATE TABLE employees( id MEDIUMINT NOT NULL  AUTO_INCREMENT,
    emp_id Varchar(30),
    age Number,
    status char(1),
    PRIMARY KEY (id) )
   
For Create table in MongoDB:
—————————-
db.employees.insert( {
    emp_id: “RAM”,
    age: 50,
    status: “A”
 } )

 Or

 db.createCollection(“employees”);

Add column in SQL:
———————
ALTER TABLE employees ADD join_date DATETIME

Add column in collection of MongoDB:
————————————
db.employees.update(
    { },
    { $set: { join_date: “” } },
    { multi: true }
)

Remove column in SQL:
———————-
alter table employees drop column join_date

Remove column in MongoDB:
—————————
db.employees.update(
    { },
    { $unset: { join_date: “” } },
    { multi: true }
)

Drop table SQL:
—————-
drop table users:

Drop collection in Mongdb:
—————————
In MongoDB:
db.users.drop();

Advertisements