if user sends any data - that data have to store permanently for that purpose we w ill use database
sql databases - oracle, - sql we will use
in nodejs json is the compatible formati to storing and retrieving
in this we will go for non sql database called mongodb
sql table
non sql - colection
in colection we wills tor docoment means json
to more secure - we stored json in binary
what is the use of mongodb
it is scalable
mongoose translator between node js and mongodb
instalation of mongodb
npm i mongoose
it will reflect in package.json in dependencies json
taht version reflect in package
install mongo db in linux
sudo service mongod stop
Remove Packages
sudo apt-get purge mongodb-org*
Remove the folders
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
2 - Reinstall as described on official site, I will just write down the all steps. enter link description here
Import the public key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
Create a list file for Ubuntu 16.04
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
update the list
sudo apt-get update
Install the latest package
sudo apt-get install -y mongodb-org
3 - Now it should work, please try below command
sudo service mongod start
and check the status
mongo
above are not working so install again
install mongo db in another
sudo apt-get install mongodb
sudo service mongodb start
to start mongo shell write below command
mongo
type the command
it will start shell run commnds
show dbs
it will show what the db u have
TO CREATE NEW DB
USE DBNAME
use newDB
if u type
db
it will show ur db
if u type
showdbs
it will show already existed db not ur db
because u not created collections;
to delete database
ist swithc to ur database then
use below command
db.dropDatabase()
db
db.createCollection(name,coletion)
db.createCollection(<name>, {capped: <boolean>,
autoIndexId: <boolean>, size: <number>, max: <number>,
storageEngine: <document>, validator: <document>,
validationLevel: <string>, validationAction: <string>,
indexOptionDefaults: <document>, viewOn: <string>,
pipeline: <pipeline>, collation: <document>, writeConcern: <document>})
db.createCollection("employee",{size:524800,max:100,capped:true}) { "ok" : 1 }
db.collectionnae.insert(document)
document should be in json format
to see the collection of new ly created
databse
to get whats the tables there
db.employee.find()
db.employee.find().pretty();
use db.employee.find()
it will show the uniquer character with saved data
db.coollectionnane.find()
to display the read in beter use pretty
db.employee.find().pretty()
show collections command used to get the tables
use pretyy
get age of 20 whoc citiy is london
db.employee.find({city:"lonodon"})
get tje salary 15000 less han
or age less tahn 15
write the query
select name,age,city from employee where age<15
db.employee.find({age: {$lt: 15}},{city:1,name:1,age:1});
$lt - lesser than
$gt - greter than
beaturiful way uisng pretty
db.employee.find({age: {$lt: 15}},{city:1,name:1,age:1}).pretty();
till now we seen create of database
create of table or collection
reed and filter that table
to update colection use
update - for one document
updateMany - for all document
db.employee.update({name:"vivek"},($set:{age:23}))
db.employee.update({age:3},{$set:{age:23}});
it will marched age 3 wth age 23
if we wanna update all matched rows
use updateMany
db.employee.updateMany({age:2},{$set:{age:5}});
write the queries for salary increase by 2 where city is mumbiai
> db.employee.updateMany({city:"mumbai"},{$mul:{salary:1.02}});
$set, $mul
deletion of document in mongodb
db.collection_name.remove(selection logic)
$eq- equal
$set- update
$lt - less tahn
$gt greter than
db.employee.remove({age:{$eq :20}})
$mul multipy
how to delee complete table
db.employee.remove({})
sudo apt-get install mongodb
sudo service mongodb start
mongo
show dbs
db.dropDatabase()
db.createCollection(name,coletion)
db.createCollection("employee",{size:524800,max:100,capped:true})
db.collectionnae.insert(document)
db.employee.find()
db.employee.find().pretty()
db.employee.find({age: {$lt: 15}},{city:1,name:1,age:1}).pretty();
$lt
$gt
$set
db.employee.update({age:3},{$set:{age:23}});
db.employee.updateMany({age:2},{$set:{age:5}});
db.employee.updateMany({city:"mumbai"},{$mul:{salary:1.02}});
db.employee.remove({age:{$eq :20}})
db.employee.remove({})
No comments:
Post a Comment