_id which is a primary key of type ObjectIDSyntax:
{
"key":value,
"key":value,
"key":value
}
Example :
{
"_id": 1,
"name": "AC3 Phone",
"colors" : ["black", "silver"],
"price" : 200,
"available" : true
}
mongodb://username:password@host[:port]?<options>
login to atlas account
atlas auth login
list all the databases :
show dbs
list all collections
show collections
create and/or use database :
use <databaseName>
check mongodb status
db.hello()
create a new collection :
db.createCollection('collectionName')
select the collection
db.<collection>
insert one document to the collection :
db.<collection>.insertOne
{ key1: "value1", key2: "value2" }
Example : db.accounts.insertOne({name: "John Doe",age: 30,})
insert multiple documents to the collection
db.<collection>.insertMany([
{ key1: "value1", key2: "value2" },
{ key1: "value3", key2: "value4" },
{ key1: "value5", key2: "value6" }
])
Example :
db.accounts.insertOne([
{name: "John Doe",age: 30,},
{name: "Albert Einstein",age: 80,}
])
find documents
db.<collection>.find({
<field> : <value>
})
Example :
db.sales.find(
{ id: ObjectId('5bd761dcae323e45a93ccff4') }
)
use $eq to select document with specified value (alternative to above query)
db.<collection>.find({
<field> : {
$eq : <value>
}
})
Example :
db.sales.find(
{ _id:
{ $eq: ObjectId('5bd761dcae323e45a93ccff4') }
}
)
Use $in to select documents with values in the array
db.<collection>.find({
<filed>:{
$in:[<value>,<value>,....]
}
})
Example :
db.sales.find(
{ storeLocation :
{ $in: ["London", "New York"] }
}
)
These can be used with find commands
$gt greater than$lt less than$lte less than or equal to$gte greater than or equal toExample :
db.sales.find(
{ "customer.age" :
{ $gt: 50 }
}
)
Find method will not only looks for scalar values, but it can also look through arrays for matching values
$elemMatch will find all document that contains the sub document
kind of subquery in sql
db.<collection>.find({
<field> : {
$elemMatch :{
<query>
}
}
})
Example :
db.accounts.find(
{ "produce" :
{ $elemMatch :
{ $eq : "InvestmentStock" }
}
}
)
$and : returns all the documents satisfying the expressions
db.<collection>.find({
$and:[
{ <expression> },
{ <expression> },
....
]
})
Example :
db.sales.find(
{ $and : [
{ purchaseMethod : "Online" },
{ couponUsed : true },
{ "customer.age" :
{ $lte : 25 }
}
] }
)
it has an implicit syntax : db.collection.find( {<expression>, <expression>} ) , here , acts as $and
$or : returns the document that satisfies at least one of the expression
db.<collection>.find({
$or:[
{<expression>},
{<expression>},
....
]
})
Example :
db.sales.find(
{ $or : [
{ "items.name" : "pens" },
{ "items.tags" : "writing" }
] }
)
We can use these logical operators together also.
Replace a single document with replaceOne()
db.<collection>.replaceOne(
<filter>, <replacement>, {options}
)
Example :
db.birds.replaceOne(
{ _id : ObjectId("6286809e2f3fa87b7d86dccd") },
{ "common_name" : "Morning Dove", "scientific_name": "Zenaida macroura" }
)
db.<collection>.updateOne(
<filter>, <update>, {options}
)
$set : used to add new fields or replace the existing value of a fielddb.podcasts.updateOne(
{ _id : ObjectId("6286809e2f3fa87b7d86dccd") },
{ $set : { subscriber : 98645 } }
)
$push : appends a value to an array or, adds the array field with the value as its element if array is not presentdb.podcasts.updateOne(
{ _id : ObjectId("6286809e2f3fa87b7d86dccd") },
{ $push : { hosts : "Nick rooby" } }
)
db.podcasts.updateOne(
{ title : "Im inevitable" },
{ $set : { topics : "action" } },
{ upsert : true }
)
db.<collection>.updateOne(
<filter>, <update>, {options}
)
Example
db.books.updateMany(
{ year : 2023 },
{ $set : { instock : true }}
)
db.<collection>.findAndModify({
query: <filter>,
update: <update>,
new: <boolean value>
})
new set to true will return the updated documentdb.birds.findAndModify({
query: { common_name: "Blue Jay"},
update: { $inc : { sightings_count:1 } },
new :true,
})
Delete one document
db.collection.delete(
<filter>, {options}
)
Example :
db.birds.deleteOne({
_id: ObjectId("62cddf53c1d62bc45439bebf")
})
Delete multiple documents
db.collection.deleteMany(
<filter>, {options}
)
Example :
db.birds.deleteMany({
sightings_count:{
$lt : 10
}
})
find() method returns a cursordb.collection.find(<query>).sort(<sort>)
Example :
db.companies.find(
{category_code:"music"}
)
.sort(
{name:1}
)
find() method is sorted by name attribute in ascending order.1 specifies ascending order, 0 specifies descending orderlimit the result set to specified number of documents
db.collection.find(<query>).limit(<number>)
db.collection.find(
<query>, <projection>
)
to include a filed use 1 and to exclude a field use 0
below example includes the field1 and excludes field2
db.collection.find( <query>, {<field1>:1,<field2>:0})
Example :
db.sales.find(
{storeLocation:"Denver"},
{saleDate:1,storeLocation:1,purchaseMethod:1, _id:1})
to count no of documents
db.collection.countDocuments(
<query>,
<options>
)
Example :
db.sales.countDocuments(
{couponUsed:true, storeLocation:"Denver"}
)
db.collection.aggregate([
{ $stage_name: {<expression >} },
{ $stage_name: {<expression >} }
])
different stages are
$match : filters the data that matches criteria, similar to find
{
$match:<Expression>
}
$group : group document based on criteria
{
$group:{
_id:<expression>, //group key
<field>:{<accumalator>:<expression>}
}
}
_id specifies on which field the aggregate has to be performed<field> specifies the name given to aggregate result<accumalator> is a stage or aggregate function like $count, $sum etcdb.zip.aggregate([
{
$match:{state:"CA"}
},
{
$group:{
_id:"$city,
totalZip:{$count:{}}
}
}
])
$sort : puts the document in a specified order
1 for ascending order, -1 for descending order{
$sort: {
"field_name": 1
}
}
$limit : limits the no. of documents
{
$limit: 5
}
Example :
db.zips.aggregate([
{
$sort:{
pop:-1
}
}
{
$limit:3
}
])
$project : similar to find(), use as the last stage
{
$project:{
<field>:<value>,
<field>:<value>,
.....
}
}
Example :
db.sightings.aggregate([
{
$project:{
date:1,
species_common:1,
_id:0
}
}])
<value> is set 1 to include, and 0 to exclude$set : add or modify the fields in the pipeline
{
$set:{
<field>:<value>,
<field>:<value>,
.....
}
}
Example :
db.birds.aggregate([
{
$set:{
class: 'bird'
}
}
])
$count : count no of documents in the pipeline
{
$count:<field>
}
$out : write documents that are returned by an aggregate pipeline into a new collection, it should be a last stage.replaces existing data, or creates new data
{
$out:{
db: <db>,
coll:<newCollection>
}
}
db is not specified then it replaces the existing collection or dbData that is accessed together should be stored together
{
_id:...,
title:"RRR",
director:"SS Rajamouli"
}
{
_id:...,
title:"RRR",
cast:[
{actor:"Jr. NTR", character:"Bhim"}
{actor:"Ram Charan Tej", character:"Lead role"}
],
...
}
{
_id:...,
title:"RRR",
cast:[
{actor:"Jr. NTR", character:"Bhim"}
{actor:"Ram Charan Tej", character:"Lead role"}
],
...
}
_id field for referencing{
_id:...,
title:"RRR",
cast:[
ObjectID("456fner63e343nc3e34),
ObjectID("456fner63rt24e43334),
],
...
}
Its performing multiple operations on multiple document or collection within a single transaction.
Session : used to group db operations that are related to each other & should run together
Max runtime for transaction is 1min
Steps in multi document transaction :
create a session
const session=db.getMongo().startSession()
start session
session.startTransaction()
choose database & collection then create account variable
const account=session.getDatabase(<database>).getCollection(<collection>)
perform the db operations like updateOne()
account.updateOne({<filter},{<query>}) // perform on first document
account.updateOne({<filter},{<query>}) // perform in second document
commit transactions
session.commitTransactions()
aborting transaction
session.abortTransaction()
special data structure to store small portion of the data
ordered and easy to search efficiently
points to document identity
improves query performance
default index includes _id
createIndex() is used to create an index
getIndexes() is used to list the index
explain() is used to check whether index is being used or not
hideIndex(<index>) can be used to hide the index
dropIndex() is used to delete or drop the index
dropIndexes() is used to delete all index
Index types
db.collection.createIndex({fieldName:1})
db.collection.createIndex({<fieldName>:1, <fieldName>:1, ...})
db.collection.createIndex({<fieldName of array>:1, <fieldName>:1, ...})
{
$search:{
"index":<index-name>,
<operator-name> | <collector-name>:{
<operator-specification> | <collector-specification>
},
"highlight":{
<highlight-option>
},
"count":{
<count-option>
},
"returnStoredSource": true | false
}
}
$searchMeta allows us to see the facets and how many results are in each buckets