Notes 3
Notes 3
MongoDB
Using MongoDB
Open MongoDB in the terminal:
Write mongosh in your terminal, and it will open the
MongoDB console.
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 1 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
show databases;
JavaScript
show dbs;
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 2 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 3 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
use name_of_database;
JavaScript
show collections;
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 4 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
db.collectionname.find();
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 5 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
use new_database_name;
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 6 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 7 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
db.createCollection("name_of_the_collec
tion")
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 8 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 9 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
db.collectionName.insertOne({key1:
value1, key2: value2 ....})
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 10 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
db.collectionName.insertMany([
{
key1: value1,
key2: value2
},
{
key1: value1,
key2: value2,
key3: value3,
.
.
.
},
{
key5: value5,
key7: value7,
.
.
}
]);
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 11 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 12 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 13 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 14 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
two options:
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 15 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 16 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 17 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 18 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 19 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
db.collectionName.find().count();
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 20 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
db.collectionName.find().limit(no_of_re
cords_to_fetch);
mongoshmongodb:|/127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000(I?directConnection=true\&se)961 ~/Downloads
new_weather_db>db.weather_data.find().limit(3)
{
_id:ObjectId("5553a998e4b02cf7151190b9"),
st:'x+45200-066500',
ts:ISODate("1984-03-05T14:00:00.000Z"),
position:{type:'Point',coordinates:1-66.5,45.21},
elevation:9999,
callLetters:'VC81',
qualityControlProcess:'V020',
dataSource:'4'
type:'FM-13'
airTemperature:{value:-4.7,quality:'1'},
dewPoint:{value:999.9,quality:'9'3,
pressure:€value:1025.9,quality:'1'},
wind:{
direction:Iangle:999,quality:'9'3,
type:'9',
speed:<rate:999.9,quality:'9'}
},
visibility:{
distance:{value:999999,quality:'9'3,
How to set an offset while
variability:{value:'N',quality:'9'}
3,
querying data?
skyCondition:{
ceilingHeight:{value:99999,quality:'9',determination:'9'3,
cavok:'N'
},
Wesections:['AGI'1,
can set an offset using the skip function.
precipitationEstimatedObservation:discrepancy:'2',estimatedWaterDepth:999}
3,
Using the skip function we can skip some records
and then start fetching records post the skip.
JavaScript
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 22 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
db.collectionName.find().skip(5).limit(
3)
JavaScript
db.collectionName.find({key1: value1,
key2: value2})
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 23 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
Projections
If we want to not get all the properties of the
JSON, and instead get some specific key-value
pairs, this process is called Projection. In the world
of SQL, if you do SELECT * FROM TABLE; then you
get all the columns but if you do SELECT name,
address FROM TABLE; you only get name and
address. This same thing is achieved in Projections.
How to do projections?
JavaScript
db.collectionName.find({filter1:
value1...}, {property1: true,
property2: true....});
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 24 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
db.collectionName.deleteOne({filter1:
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 25 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
db.collectionName.deleteMany({filter1:
value1, filter2: value2..});
new_weather_b>db.weather_data.deleteOne({_id:ObjectId("5553a998
{acknowledged:true,deletedCount:1}
new_weather_db>db.weather_data.find(f_id:ObjectId("5553a998e4b02
newweatherdb>
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 26 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
db.collectionName.updateOne({filter1:
value1}, {$operator: {key1: value1,
key2: value2...}})
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 27 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
new-_weather_b>db.weather_data.updateOne(<_id:ObjectId("5553a998e4b02cf7151190c0")},
{
acknowledged:true,
insertedId:null,
matchedCount:1,
modifiedCount:1,
upsertedCount:0
JavaScript
db.collectionName.distinct("key");
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 28 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
Weather>db.weather_data.distinct("type")
['FM-13', 'SAO'1
Weather>db.weather_data.distinct("callLetters")
'OBVZ' 'OJSV', '2100'"3DRO'. '3DRV' "ЗЕВА'
"ЗЕЕК" "BEFT' "BEFO' "BEGC"
'BEGU'
"ЗЕНН' '3EIZ 'BEJF' 'BELE'.
'BEOS' 'BEOU' "ЗЕРА' 'BEON' "BERJ' '3ERZ'
"3EWO", "3EXD 'BEYN' '3EZL', '3FAR' "3FBD'
"3FEV' '3FGE', '3FJR', "ЗЕКА' "3FMV'
'3FTJ' '3FXH', "3FXS' "3FYO
'ДРКУ" "AXIC' 'AXII' 'AXIL' 'AXLU 'AXLZ'
'5LEX' 15LJT' '5LLP' 15LOT' 15L0Z '5LTW'
'5LWE'.'5LWG' '5LWO' "5MCB'. "5MCN '5MGC',
'5MTS' '5MZH' '5PVN', '67AM' '6ZAS' '67BM'
Operators
'6ZDN' '6ZFM' in MongoDB
'6ZHX', '6ZJP' '6ZLH' '6ZRT'
'7FFC' "7JEY' "7JKS' "ZIKY' "7JOB'
MongoDB '7J0x',
"7JOI'provides 'ZIRZ',
different '7J50',
comparison, "7JSP 171т11
logical,
'ZJUI',
bitwise JVMthat
etc operators
'7
', we can use very easily.
...2549moreitems
To use these operators for comparison and logical
operations, instead of directly assigning a key-
value pair for your filters etc, put your key and
assign it an object, inside the assigned object, put
the key as your operator and value as the value to
put.
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 29 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JavaScript
db.collectionName.find({property:
{$operator: value}})
Weather>db.weather_data.find({type:{$ne:'FM-1
6
Weather>
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 30 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
Weather>db.weather_data.find({callLetters:f$in:['ЗЕН','EIZ',
17
Weather>
Analysing queries
We can analyse our queries using the explain
function. Inside this function, we can pass an
argument executionStats and it will provide us
with the details
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 31 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
Bash
db.weather_data.find({type:
'SAO'}).explain("executionStats")
JSON
{
explainVersion: '1',
queryPlanner: {
namespace: 'Weather.weather_data',
indexFilterSet: false,
parsedQuery: { type: { '$eq': 'SAO'
} },
maxIndexedOrSolutionsReached:
false,
maxIndexedAndSolutionsReached:
false,
maxScansToExplodeReached: false,
winningPlan: {
stage: 'COLLSCAN',
filter: { type: { '$eq': 'SAO' }
},
direction: 'forward'
},
rejectedPlans: []
},
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 32 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
executionStats: {
executionSuccess: true,
nReturned: 6,
executionTimeMillis: 31,
totalKeysExamined: 0,
totalDocsExamined: 10000,
executionStages: {
stage: 'COLLSCAN',
filter: { type: { '$eq': 'SAO' }
},
nReturned: 6,
executionTimeMillisEstimate: 0,
works: 10002,
advanced: 6,
needTime: 9995,
needYield: 0,
saveState: 10,
restoreState: 10,
isEOF: 1,
direction: 'forward',
docsExamined: 10000
}
},
command: { find: 'weather_data',
filter: { type: 'SAO' }, '$db':
'Weather' },
serverInfo: {
host: 'Sankets-MacBook-Pro.local',
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 33 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
port: 27017,
version: '5.0.7',
gitVersion:
'b977129dc70eed766cbee7e412d901ee213acb
da'
},
serverParameters: {
internalQueryFacetBufferSizeBytes:
104857600,
internalQueryFacetMaxOutputDocSizeBytes
: 104857600,
internalLookupStageIntermediateDocument
MaxSizeBytes: 104857600,
internalDocumentSourceGroupMaxMemoryByt
es: 104857600,
internalQueryMaxBlockingSortMemoryUsage
Bytes: 104857600,
internalQueryProhibitBlockingMergeOnMon
goS: 0,
internalQueryMaxAddToSetBytes:
104857600,
internalDocumentSourceSetWindowFieldsMa
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 34 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
xMemoryBytes: 104857600
},
ok: 1
}
Indexing
Indexing is a mechanism using which a database
prepared more data structures to store data in a
particular order based on particular keys, for faster
search.
Weather>db.weather_data.getIndexes()
{v:2,key:{_id:13,name:'_id_'}
{v:2,key:{elevation:1},name:'elevation1'
V:2,
key:{elevation:1,'airTemperature.value':1},
name:'elevation_1_airTemperature.value_1'
},
To{v:2,key:{'dewPoint.value':1},name:'dewPoi
remove an index from the
collection
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 35 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
Weather>ob.weather_data.dropIndex\'elevation_1')
{nIndexesWas:4,ok:1}
Weather>db.weather_data.getIndexes()
{v:2,key:{._id:17,name:_id_'3,
V:2,
key:{elevation:1,'airTemperature.value':1},
name:'elevation_1_airTemperature.value_1
},
To{v:2,key:{'dewPoint.value':1},name:'dewPoint
create an index for a collection
new_weather_db>db.weather_data.createIndex(
elevation1
Analysing indexes
Before we created our index on the elevation
property we were reading all the docs i.e. approx
9991 docs, to fetch all the docs with elevation less
than 5000.
JSON
{
explainVersion: '1',
queryPlanner: {
namespace:
'new_weather_db.weather_data',
indexFilterSet: false,
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 36 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 37 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
advanced: 6,
needTime: 9986,
needYield: 0,
saveState: 9,
restoreState: 9,
isEOF: 1,
direction: 'forward',
docsExamined: 9991
}
},
command: {
find: 'weather_data',
filter: { elevation: { '$lt': 8000
} },
'$db': 'new_weather_db'
},
serverInfo: {
host: 'Sankets-MacBook-Pro.local',
port: 27017,
version: '5.0.7',
gitVersion:
'b977129dc70eed766cbee7e412d901ee213acb
da'
},
serverParameters: {
internalQueryFacetBufferSizeBytes:
104857600,
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 38 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
internalQueryFacetMaxOutputDocSizeBytes
: 104857600,
internalLookupStageIntermediateDocument
MaxSizeBytes: 104857600,
internalDocumentSourceGroupMaxMemoryByt
es: 104857600,
internalQueryMaxBlockingSortMemoryUsage
Bytes: 104857600,
internalQueryProhibitBlockingMergeOnMon
goS: 0,
internalQueryMaxAddToSetBytes:
104857600,
internalDocumentSourceSetWindowFieldsMa
xMemoryBytes: 104857600
},
ok: 1
}
JSON
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 39 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
JSON
{
explainVersion: '1',
queryPlanner: {
namespace:
'new_weather_db.weather_data',
indexFilterSet: false,
parsedQuery: { elevation: { '$lt':
8000 } },
maxIndexedOrSolutionsReached:
false,
maxIndexedAndSolutionsReached:
false,
maxScansToExplodeReached: false,
winningPlan: {
stage: 'FETCH',
inputStage: {
stage: 'IXSCAN',
keyPattern: { elevation: 1 },
indexName: 'elevation_1',
isMultiKey: false,
multiKeyPaths: { elevation: []
},
isUnique: false,
isSparse: false,
isPartial: false,
indexVersion: 2,
direction: 'forward',
indexBounds: { elevation: [ '[-
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 40 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
inf.0, 8000)' ] }
}
},
rejectedPlans: []
},
executionStats: {
executionSuccess: true,
nReturned: 6,
executionTimeMillis: 2,
totalKeysExamined: 6,
totalDocsExamined: 6,
executionStages: {
stage: 'FETCH',
nReturned: 6,
executionTimeMillisEstimate: 0,
works: 7,
advanced: 6,
needTime: 0,
needYield: 0,
saveState: 0,
restoreState: 0,
isEOF: 1,
docsExamined: 6,
alreadyHasObj: 0,
inputStage: {
stage: 'IXSCAN',
nReturned: 6,
executionTimeMillisEstimate: 0,
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 41 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
works: 7,
advanced: 6,
needTime: 0,
needYield: 0,
saveState: 0,
restoreState: 0,
isEOF: 1,
keyPattern: { elevation: 1 },
indexName: 'elevation_1',
isMultiKey: false,
multiKeyPaths: { elevation: []
},
isUnique: false,
isSparse: false,
isPartial: false,
indexVersion: 2,
direction: 'forward',
indexBounds: { elevation: [ '[-
inf.0, 8000)' ] },
keysExamined: 6,
seeks: 1,
dupsTested: 0,
dupsDropped: 0
}
}
},
command: {
find: 'weather_data',
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 42 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
internalQueryFacetMaxOutputDocSizeBytes
: 104857600,
internalLookupStageIntermediateDocument
MaxSizeBytes: 104857600,
internalDocumentSourceGroupMaxMemoryByt
es: 104857600,
internalQueryMaxBlockingSortMemoryUsage
Bytes: 104857600,
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 43 of 44
Editing "MongoDB" 28/05/23, 7:22 PM
internalQueryProhibitBlockingMergeOnMon
goS: 0,
internalQueryMaxAddToSetBytes:
104857600,
internalDocumentSourceSetWindowFieldsMa
xMemoryBytes: 104857600
},
ok: 1
}
https://hashnode.com/draft/6471f3c0de2159000f618c28 Page 44 of 44