- developer data platform
- atlas handles itself
- built-in data replication (replica sets)
- It's a
DBaasi.e. Database as a service - stores data on multiple servers
- Ensures data redundancy in case of server failure
- Serverless :
- scales on demand
- charges for resources used
- used for highly variable workload overtime
- simple to use
- Clusters :
- Shared :
- Contains free tier options
- smaller size
- good for small projects
- Dedicated :
- customizable
- enhanced security features
- scalable
- Shared :
- Cloud providers : GoogleCloud, AWS, Azure
- Over 100 geographical regoins
- Change as you scale
- Operational insights
- Backup with point-in-time restore
- Online archive
- Atlas charts for dashboards
- Data API in-built
- GraphQL API
<>indicates - do as mentioned
Table of Contents
- Showing Databases
- Using / Creating Database
- Showing collections
- Creating Collections
- Inserting Documents
- Searching in Document
- Replacing Documents
- Updating Documents
- Deleting Documents
- Sorting and Limiting
- Counting Documents
- Aggregation
- Indexes
show dbsuse <dbname>show collectionsdb.createCollection('<collection name>')- to insert one document use
insertOne()
db.grades.insertOne( {
student_id: 123,
scores: [
{
subject: 'Maths',
marks: 78
},
{
subject: 'Science',
marks: '89'
}
]
} )- there comes a acknowlegment after this with
ObjectIdif not specifies
- use
insertMany()
db.grades.insertMany( {
student_id: 123,
scores: [
{
subject: 'Maths',
marks: 78
},
{
subject: 'Science',
marks: 89
}
]
}, {
student_id: 124,
scores: [
{
subject: 'Maths',
marks: 58
},
{
subject: 'Science',
marks: 91
}
]
}, {
student_id: 125,
scores: [
{
subject: 'Maths',
marks: 23
},
{
subject: 'Science',
marks: 45
}
]
} )- Syntax
db.<collection-name>.find()- Example for
$eq
db.zips.find({ states: "MH" })- Example for
$infor seraching from array
db.zips.find({ city: { $in: ["Pune", "Mumbai"] } })$gt-> greater than$gte-> greater than equal to$lt-> less than$lte-> less than equal to
db.sales.find({ "items.price": { $gt: 50 } })items.priceindicate nested fields in document
db.sales.find({ "customer.age": { $gte: 65 } })$elemMatch-> Used to query on array elements
db.accounts.find( {
products: {
$elemMatch: { $eq: "InvestmentStock" }
}
} )db.sales.find( {
items: {
$elemMatch: { name: "laptop", price: { $gt: 800 }, quantity: { $gte: 1 } }
}
} )$and,$oroperators
- both mixed
When including same operator more than once
in your query, you need to use the explicit $and operator
- Syntax : -
db.<collection-name>.replaceOne(filter, replacement, options)
- Example : -
db.books.replaceOne( { _id: ObjectId("65s863ss4s58s963s24s53") }, { title: "Deep dive in React Hooks", ISBN: "0-358-4562-8", thumbnail: "http://via.placeholder.com/640x360", publicationDate: ISODate("<some date>"), authors: ["Ada lovelace"] } )
Beware that replaceOne will replace whole document not append or update it
-
Syntax : -
db.collection.updateOne( <filter>, <update>, { options } )
-
$setadds new value to document or replaces it if exists -
$pushappends value to an array or adds array field with specified value if absent -
Example : -
db.podcast.updateOne( { _id: ObjectId("8f6y2d9d3ge2ds5sff5wf5wf212") }, { $set: { subscribers: 96026 } } )
-
upsertallows to insert document with provided values if one doesn't exists else updates as usualdb.podcast.updateOne( { _id: ObjectId("8f6y2d9d3ge2ds5sff5wf5wf212") }, { $set: { subscribers: 96026 } } { upsert: true } )
-
$pushexampledb.podcast.updateOne( { _id: ObjectId("6ku9f4s2safd3vsfnthn65d") }, { $push: { hosts: "Nic Raboy" } } ) # hosts is an array here
- update multiple documents
- accepts
{filter},{update},{options}as usual - below command sets all books before 2019 as legacy books status
db.books.updateMany(
{ publishedDate: { $lt: ISODate("2019-01-01T08:00:00.000Z") } },
{ $set: { status: "LEGACY" } })- not all-or-nothing operation
- will not roll-back updates
- updates will be visible as soon as they are performed
- not appropriate for some some use cases like business / financial operations
findAndModify()returns the document that just has been updated- Instead of
updateOne()thenfindOne(),findAndModify()is better as previous takes 2 db calls while latter takes just 1 db call and in previous method another thread may change the document beforefindOne()which is not good - Example
db.podcast.findAndModify( query: { _id: ObjectId("8f6y2d9d3ge2ds5sff5wf5wf7854") }, update: { $inc: { downloads: 1 } }, new: true )
- accepts
{filter}, {options}
db.podcasts.deleteOne({ _id: ObjectId("63o696d6f336sa5a33s65s2a3a6x5") })db.podcasts.deleteMany({ category: "crime" })cursoris pointer to result set of querydb.collection.find()returns a pointer to result which is a cursor- cursor methods are chained to queries using
.operator by which we can perform sorting and limiting - Example
db.companies.find({ category: "music" }, { name: 1 }).sort({ name: 1 })
- Here
{name: 1}after category in find is a projection now document returns names only - IN sort method
1is used for ascending order and-1for descending order - Capitalized letters are sorted first then lowercase are grouped and sorted (can be changed in options)
- limiting no. of documents returned as per requirement can enhance query performance
- Example
db.companies.find({ category: "music" }, { name: 1, number_of_employees: 1 }) .sort({ number_of_employees: -1 }) .limit(3)
- Above example gives names of top 3 music companies with highest no. of employees
-
1: include0: exclude
- Example
db.inspection.find({ sector: "Restaurant - 818" }, { name: 1, result: 1, _id: 0 })- Result : -
Inclusion and exclusion cant be combined in projections with exception of _id field
.countDocuments()is used to count number of documents<query> and <options>are accepted- Example :
db.trips.countDocuments() >> 10000
db.trips.countDocuments({ tripDuration: {$gt: 120}, usertype: "Subscriber" }) >> 7847
- Used to make multistage queries and create pipelines
Aggregation: An analysis and summary of dataStage: An aggregation operation performed on the dataAggregation pipeline: A series of stages completed one at a time, in order- Data can be
Filtered,Sorted,Grouped,Transformed - When a
$is put before field name it is to refer to it's value - Order of stages definitely matter
- Syntax : -
db.collection.aggregate([ { $stage_name: { <expression> } }, { $stage_name: { <expression> } } ])
$matchstage is placed mostly early in pipeline (syntax similar to find)
db.zips.aggregate([
{ $match: { "state": "CA" } }
])$groupstage groups document by a group key- Output is one document for each value of group key
$group: {
_id: <expression>,
<field>: { <accumulator>, <expression> }
}- Example
$sortstage is used to sort the documents as per need$limitstage limits the no. of documents returned1for ascending,-1for descending- below code shows 3 zip codes with greatest population
db.zips.aggregate([
{ $sort: { pop: -1 } },
{ $limit: 3 }
])$projectstage used to specifiy what return at last in pipeline- specify either inclusion or exclusion of fields with exception of
_idsuppresion - generally used at the last of pipeline
- you can declare new fields too
db.zips.aggregate([
{
$project: {
state: 1,
zip: 1,
population: "$pop",
_id: 0
}
}
])$setstage adds or modifies fields in the pipeline- useful when want to change existing fields in pipeline or add new ones to be used in upcoming pipeline stages
db.zips.aggregate([
{ $set: {
pop_2022: {
$round: { $multiply: [1.0031, "$pop"] }
}
}
}
])$countstage counts no. of documents in pipeline- it returns the total document count
$count: <field-name>
db.zips.aggregate([
{ $count: "total_zips" }
])>> [ { total_zips: 29367 } ]$outstage writes the documents that are returned by an aggregation pipeline into a collection- It
mustbe a last stage - Creates a new collection if doesn't exist
- If exists
$outreplaces the existing collection with new data - Syntax : -
$out: {
db: "<db>",
coll: "<new collection>"
}
// or
$out: "<new collection>" // for same database- Example
db.zips.aggregate([
{
$group: {
_id: "$state",
total_pop: { $sum: "$pop" }
}
}, {
$match: {
total_pop: { $lt: 1000000 }
}
}, {
$out: "small_states"
}
])- above query writes all the result to collection names
small_statesin same database
Double check to be sure that new collection name doesn't match an existing one unless you want to explicitly override the data else data goes brrr...
- They are used background of databases to speed up querying
- Indexes improve query performance
- Speed up queries
- Reduce disk I/O
- Reduce resources required
- Support equality matches and range-based operations and returns sorted results
- Without indexes
- MongoDB reads all documents (collection scan)
- Sorts results in memory (if required)
- With indexes
- MongoDB only fetches the document identified by the index based on the query
- Returns results faster
- There is one default index per collection, which includes only the
_idfield - Every query should use an index
- If we insert or update documents, we need to update the index Data Structure
- Delete unnecessary or redundant indexes
- Index on single field
- Support queries and sort on single field
db.collection.createIndex({ fieldname: 1 })db.collection.getIndexes()
// to get the indexes createddb.custormers.explain().find({ birthdate: { $gt: ISODate("1995-08-01") } }).sort({ email: 1 })
// .explain() used to get if any indexes used in this query






