Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

MONGODB

• A NoSQL – document oreinted database.


• Data is stored in JSON format.
• Supports ACID properties: Atomicity, Consistency, Isolation, Durability.
Create Database

The use Command


MongoDB use DATABASE_NAME is used to create database. The command will
create a new database if it doesn't exist, otherwise it will return the existing database.

Syntax

Show Databases
Drop Database

The dropDatabase() Method


MongoDB db.dropDatabase() command is used to drop a existing database.
Syntax
db.dropDatabase()

This will delete the selected database. If you have not selected any database, then it
will delete default 'test' database.
Example
>use mydb
switched to db mydb
>db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 }
The createCollection() Method

MongoDB db.createCollection(name, options) is used to create collection.

Syntax
db.createCollection(name, options)

Name String Name of the collection to be created


Options Document (Optional) Specify options about memory size and indexing
CreateCollection

Basic syntax of createCollection() method without options is as follows −


>use test
switched to db test
>db.createCollection("mycollection")
{ "ok" : 1 }

In MongoDB, you don't need to create collection. MongoDB creates collection


automatically, when you insert some document.
>db.student.insert({"name" : "Arun"})
>show collections
mycollection
system.indexes
student
Insertion of One Record
To insert data into MongoDB collection, you need to use MongoDB's insertOne() or
insertMany()
Syntax
>db.collection_name.insertOne(document)

Example
db.post.insertOne([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
}
])
Insertion of Many Records:
To insert many records into MongoDB collection, you need to use MongoDB's insertMany()
Syntax
>db.collection_name.insertMany(document1 , document2, … )

Example
db.post.insertMany([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
}, {
title: Php Overview',
description: ‘Php is server scripting language',
tags: [‘php', ‘server'],
likes: 70
},
])
Deletion of One Record
To delete data into MongoDB collection, you need to use MongoDB’s deleteOne() or
deleteMany()
Syntax
>db.collection_name.deleteOne(condition)

Example

db.movies.deleteOne( { cast : “Rajini” });

Deletes the first record with the given cast.


Deletion of Many Records:
To delete many records into MongoDB collection, you need to use MongoDB’s deleteMany()

Syntax
> db.collection_name.deleteMany(condition)

Example
db.post.deleteMany({})

This command empties the post collection.


Deletes all documents.
Updation of One Record
To update data into MongoDB collection, you need to use MongoDB’s updateOne() or
updateMany()

Syntax
>db.collection_name.updateOne(condition , set)

Example
db.post.updateOne([
{
title: 'MongoDB Overview’,
},
{
$set : { likes : 200 }
}
]);
Updation of Many Records:
To update many documents into MongoDB collection, you need to use MongoDB’s
updateMany()

Syntax
>db.collection_name.updateMany(condition , set)

Example
db.post.updateMany([
{
title: 'MongoDB’,
},
{
$set : { likes : 200 }
}
]);
The find() Method : Query Data

To query data from MongoDB collection, you need to use MongoDB's find() method.

Syntax
>db.collection_name.find()
find() method will display all the documents in a non-structured way.
Eg: db.post.find({likes : 100});

The pretty() Method


To display the results in a formatted way, you can use pretty() method.

Syntax
>db.mycol.find().pretty()
examples
• db.coll.find({"year": {$gt: 1970}})
• db.coll.find({"year": {$gte: 1970}})
• db.coll.find({"year": {$lt: 1970}})
• db.coll.find({"year": {$lte: 1970}})
• db.coll.find({name: {$exists: true}})
• db.coll.find({name: /^Max/})
• db.coll.find({}).sort({"year": 1, "rating": -1})
AND in MongoDB

In the find() method, if you pass multiple keys by separating them by ',' then
MongoDB treats it as AND condition.
Syntax

>db.mycol.find(
{
$and: [
{key1: value1}, {key2:value2}
]
}
).pretty()
OR in MongoDB

To query documents based on the OR condition, you need to use $or keyword.
syntax of OR −

>db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()

You might also like