09.Mongo DB - ALL
Mongo DB
Mongo DB යනු noSql open source database එකක් වේ. SQL database
හෙවත් relational database
වලදී data table වල තබා ගන්න
අතර මෙම table table අතර relationship ද පවතී.නමුත්
mongoDB වලදී data පවතිනුයේ document collection එකක් ලෙසට වන අතර මෙම
documents අතර relations නැත.එනම් documents are
not related with each other. Schema free වන අතර this is based on binary
JSON එනම් BSON. Mongo DB simple query language එක ලෙස simple query language එක බාවිතා කරනු ලබයි (SQL වගේ).
SQL and MongoDB Comparison.
Table – Collection
Row – Document
Column – File
Collection is not strict about what goes
in it (it’s schema-less).
Download and install mongoDB
Go to https://www.mongodb.com/try/download/community and download
mongoDB.
Note: version 4.4.1 is only support in
windows 10.If you want to install other versions of windows, please select an
older mongo dB version.
Alter install go to bellow path…
C:\Program Files\MongoDB\Server\3.6\bin
Inside bin there are 2 excitable file
can be seen.mongod.exe and
mongo.exe
· Mogod.exe.
Mongo daemon which is going to run in the background it will accept the
connections.
· Mong.exe.
command line shell from which you are going to work with your database.
Running mongoDB
If you want to run mogoDB, first you
have to run mongod.exe then run mongo.exe.
To run, open C:\Program
Files\MongoDB\Server\3.6\bin this location with cmd.
In the CMD, type monod.exe and press
enter to run.පළමු පාර run කරන විට පහත error එක පැමිණේ.
That errors like that, please create this file (C:\data\db) and re execute the mongo.exe.
Then open this location in new CMD and execute mongo.exe.
When you execute the mongo.exe bellow part
(connection accepted) can be seen on monod.exe executed cmd.
To test,
Type db and
press enter.මෙය mongo.exe එක run වන CMD හි execute කරන්න.
නමුත් ඉහත පරිදි C:\Program Files\MongoDB\Server\3.6\bin location එකෙන් CMD එක open කරගෙන run කර කර ඉන්නට අවශ්ය වෙන්නේ නැ.ඒ සදහා environment variable එක set කරන්න.ඊට පස්සේ any location එකක ඉදන් database එකට අදාළ query run කරන්න පුළුවන්.
Setting Environment variable.
Go to view advance system setting –
environment variable – then select path and double click (select path and then
click on edit) add C:\Program Files\MongoDB\Server\3.6\bin location at end. –
OK
Then open the CMD from any location and
do the above process agin.it will working fine.
ඉහත පරිදි mongoDB එක සමග CMD
එක හරහා ගණදෙනු කල හැක(query execute කල හැක).SQL මෙය මෙලෙසම වන අතර
නමුත් පහසුව පිණිස SQL සමග වැඩ කිරීමේදී GUI(graphical
user interface) use කරනු ලබයි(MySQL Workbench , XAMP,WAP වගේ ඒවා).mongoDB සමග ද පහසුවෙන් වැඩ කිරීමට මෙලෙස GUI use කර ගත හැක.
Download
and install a GUI (MongoChef)
Go to https://studio3t.com/download/ and Download and install it.
Download MongoShell
Go to https://www.mongodb.com/try/download/shell and Download.
Then extract it.
Copy and paste extracted file in C drive (C:\mongosh-1.8.2-win32-x64).
Add its bin path to environment variables(C:\mongosh-1.8.2-win32-x64\bin).
Go to C:\mongosh-1.8.2-win32-x64\bin and run mongosh.exe.
Commands
which we use in mongoDB.
Create a database
use
company // create a database
If it is already exist provide it,
otherwise create it.
View all databases
show dbs
Database එකක් අලුතෙන් create කර මෙම command එක run කලද එම අලුත් database එකට document එකක් add වන තුරු එම database එක පෙන්වන්නේ නැත.
Drop a data base
db.dropDatabase() //Dropping a current woking database
Create a collection.
View Collections
show
collections //
show created collection
View Collection
data
db.branch.find()
Drop a Collection
db.employee.drop()
// remove collection
Adding Document.
මෙහෙදී Binary
Jason Format හෙවත් BSON
එක යොදා ගනු ලබයි.
Single record.
db.branch.insert({"code":"001", "name":"branch
one"})
Multiple record.
අපි Database එකට data insert කරන විට mongoDB වලින් ඒ ඒ record එකට default ම unique id එකක් generate කරගනු ලබයි.එම ID එක අපට provide කරනටද පුළුවන් වේ.
Finding data.
find() and fIndOne() සැලකිමේදී find() මගින් entire collection එකේම search කර අදාළ සියලුම document ලබා දෙන අතර, findOne() මගින් පලවෙනි එක හොයාගත්තම document එක return කිරීම කරයි.එම නිසා find() ට වඩා findOne() එක speed වේ.
AND, OR conditions
(SQL වලනම් && සහ ||).
Updating
save වලදී id = "5f5ca367ccb1774f207b1dd7" search කරන අතර එහෙම එකක් තියනවනම් new valuess වලින් old එක update කරන අතර එහෙම එකක්
නැත්තන් new document එකක් create කරනු ලබයි.
Removing Elements.
Projection
Selecting only necessary data rather
than selection the all data from a document.
limit, skip and sort
Indexing
Indexes are the special data structure
that sores a small portion of data set to easy to pass way. Mongo database is
really large, it can have millions of records. If we want to find one document
among those it is very important to use indexing.
Adding large amount of document to test
the indexing.
Create index
db.employee.createIndex({"salary":1})
Fetch document
db.employee.find({"salary":"23000"})
Drop Index
db.employee.dropIndex({"salary":1})
MongoDB Aggregation
This groups values from multiple documents
together and can perform variety of operations on that grouped data.it is going
to return single result after the aggregation.
In
the SQL we query select count and in the bracket Astrix this count for aggregation.
It return the number of rows. This is same in mongoDB.
Bellow example is from https://docs.mongodb.com/manual/aggregation/
Single Purpose Aggregation Operations
MongoDB Backup and
Restore
Backup
Open lib location (C:\Program Files\MongoDB\Server\3.6\bin) in CMD administrative mode
To back up all the
databases
Run mongodump in CMD.
To back up a
specific databases
Run mogodump --db databasename in CMD.
Example: mogodump --db company
To back up a
specific collection.
Run mogodump --db databasename --collection collectionname
in CMD.
Example: mogodump --db company --collection employee
Restore
Open lib location (C:\Program Files\MongoDB\Server\3.6\bin) in CMD administrative mode
To restore all the
databases
Run mongorestore in CMD.
To restore a
specific databases
Run mongorestore --db databasename dump/databasename
in CMD.
Example: mongorestore --db company dump/company
To restore a
specific collection
Run mongorestore --db databasename --collection
collectionname in CMD.
Example: mongorestore --db company --collection employee
dump/employee/employee.bson
Comments
Post a Comment