DDL and DML Operations in MongoDB

Posted on by By Sayali Mahale, in Databases | 0

MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling.

Make data easy with Helical Insight.
Helical Insight is world’s best open source business intelligence tool.

Claim Your 30 Days Free Trail

A record in MongoDB is a document, which is a data structure composed of field and value pairs As:

{
name:”ABC”,
age:18,
gender:”Male”
}

MongoDB is a document-oriented DBMS. Think of MySQL but with JSON-like objects comprising the data model,
rather than RDBMS tables. Significantly, MongoDB supports neither joins nor transactions.Instead of tables, a MongoDB database stores its data in collections, A collection holds one or more documents, which corresponds to a record or a row in a relational database table, and each document has one or more fields, which corresponds to a column in a relational database table.

MongoDB uses dynamic schemas. You can create collections without defining the structure, i.e. the fields or the types of their values, of the documents in the collection.

db.createCollection(“users”)

You can change the structure of documents simply by adding new fields or deleting existing ones.

Following are some DDL+DML Statements in MongoDB:

1) To Create Table:

MongoDB => db.createCollection(“users”)

Equivalent SQL:create table users(user_id varchar2(10),name varchar2(10),age number)

2) To Insert values into Table:

MongoDB => db.users.insert({user_id:”abc33″,name:”sayali”,age:22})

Equivalent SQL: Insert into values(“abc33”,”sayali”,22)

3) To Update Table Value:

MongoDB => db.users.update( { },
{ $set: { join_date: new Date() } },
{ multi: true })

4) To Drop column from Table:

MongoDB => db.users.update( { },
{ $unset: { join_date: new Date() } },
{ multi: true } )

5) Check for Not equal Value:
MongoDB => db.users.find({ age: { $ne: 22 } })

Equivalent SQL: Select * from users Where age!=23

6) Sort operation:

To sort field by Asending and Desending order
ASC:value=1
Desc:value=-1

MongoDB => db.users.find({}).sort( { age: 1 } )

Equivalent SQL: select * from users Order by age ASC;

7) To use count() function:
MongoDB => db.users.count()

Make data easy with Helical Insight.
Helical Insight is world’s best open source business intelligence tool.

Click Here to Free Download

8) To find out Distinct column Value:

MongoDB => db.users.distinct( “age” )

9) To Delete Row:

MongoDB => db.users.remove( { status: “D” } )

These are the basic operations in MongoDB, which differs from SQL queries.

Thanks,
Sayali Mahale

logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

4.3 3 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments