Introduction To MongoDB

NoSQL Databases

Advantages of NoSQL

MongoDB Advantages

Collection

Document

{
_id: ObjectId(df78e7rgav5s41bg56e4)
name: { first: "Alan", last: "Turing" }
birth: new Date('Jun 23, 1912'),
death: new Date('Jun 07, 1954'),
contribs: [ "Turing machine", "Turing test", "Turingery" ],
views : NumberLong(1250000)
}

MongoDB CRUD Operations

1. Create Operations

db.book.insertOne( 
{
name: “JavaScript Cookbook ”,
Author: Adam D. Scott,
price: 100.00,
edition: 3,
tags: ["Javascript","Ecmascript"]
}
)
db.book.insertMany([ 
{
name: “JavaScript Cookbook ”,
Author: Adam D. Scott,
price: 100.00,
edition: 3,
tags: ["Javascript","Ecmascript"]
},
{
name: “JavaScript The Definitive Guide ”,
Author: David Flanagan,
price: 200.00,
edition: 7,
tags: ["Javascript","Ecmascript"]
},
{
name: “Modern JavaScript for the Impatient ”,
Author: Cay S. Horstmann,
price: 100.00,
edition: 3,
tags: ["Javascript","Ecmascript"]
}
])

2. Read Operations

db.collection.find()
db.book.find( { name: "JavaScript The Definitive Guide" } )

3. Update Operations

db.collection.updateOne(filter, update, options)db.boook.updateOne({_id:1}, { $set: {name:'Javascript Cookbook'}})
db.collection.updateMany(filter, update, options)db.boook.updateMany({name:'Javascript Cookbook'}, { $set: {price: 300.00}})
db.collection.replaceOne(filter, replacement, options)db.boook.replaceOne(
{
name: “JavaScript Cookbook ”,
Author: Adam D. Scott,
price: 100.00,
edition: 3,
tags: ["Javascript","Ecmascript"]
},
{
name: “JavaScript Cookbook ”,
Author: Adam D. Scott,
price: 200.00,
edition: 4,
tags: ["Javascript","Ecmascript"]
}
);

4. Delete Operations

db.collection.deleteOne(filter, option)db.book.deleteOne({ _id: 1 })
db.collection.deleteOne(filter, option)
db.book.deleteMany({ "price": { $gt: 200.00 } })

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store