Showing posts with label monodb. Show all posts
Showing posts with label monodb. Show all posts

mongo db summary in nodejs |nodejs tutorials

 const {it}=require("node:test")


sudo apt-get install mongodb
sudo service mongodb start
mongo
show dbs

db - to find u r in which db
db.dropDatabase()

db.createCollection(name,coletion)
db.createCollection("employee",{size:524800,max:100,capped:true})

db.collectionnae.insert(document)
db.collectionnae.insertOne(document)

db.posts.insertOne({
title: "Post Title 1",
body: "Body of post.",
category: "News",
likes: 1,
tags: ["news", "events"],
date: Date()
})


db.posts.insertMany([
{
title: "Post Title 2",
body: "Body of post.",
category: "Event",
likes: 2,
tags: ["news", "events"],
date: Date()
},
{
title: "Post Title 3",
body: "Body of post.",
category: "Technology",
likes: 3,
tags: ["news", "events"],
date: Date()
},
{
title: "Post Title 4",
body: "Body of post.",
category: "Event",
likes: 4,
tags: ["news", "events"],
date: Date()
}
])


db.employee.find()

db.employee.find().pretty()

db.employee.find({age: {$lt: 15}},{city:1,name:1,age:1}).pretty();

db.posts.findOne() -matches first one


$lt
$gt
$set

update one row which is matched first
db.employee.update({age:3},{$set:{age:23}});
db.posts.updateOne( { title: "Post Title 1" }, { $set: { likes: 2 } } )

update it if not found insert it

db.posts.updateOne(
{ title: "Post Title 5" },
{
$set:
{
title: "Post Title 5",
body: "Body of post.",
category: "Event",
likes: 5,
tags: ["news", "events"],
date: Date()
}
},
{ upsert: true }
)


update alll rows matched
db.employee.updateMany({age:2},{$set:{age:5}});
db.employee.updateMany({city:"mumbai"},{$mul:{salary:1.02}});

delete


db.employee.remove({age:{$eq :20}})

db.employee.remove({})

db.posts.deleteOne({ title: "Post Title 5" })

db.posts.deleteMany({ category: "Technology" })

db.getCollectionNames()

query parameters

$eq: Values are equal
$ne: Values are not equal
$gt: Value is greater than another value
$gte: Value is greater than or equal to another value
$lt: Value is less than another value
$lte: Value is less than or equal to another value
$in: Value is matched within an array
Logical
The following operators can logically compare multiple queries.

$and: Returns documents where both queries match
$or: Returns documents where either query matches
$nor: Returns documents where both queries fail to match
$not: Returns documents where the query does not match
got errror

Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
g


dpkg -l | grep mongo 2065 sudo apt purge mongodbb 2066 sudo apt purge mongodb 2067 dpkg -l | grep mongodb 2068 sudo apt purge mongodb-clients 2069 dpkg -l | grep mongodb 2070 sudo apt purge mongodb-server-core 2071 sudo apt-get autoremove 2072 dpkg -l | grep mongodb 2073 sudo apt-get remove mongodb* --purge 2074 sudo apt-get purge mongodb-org* 2075 sudo rm -r /var/log/mongodb 2076 sudo rm -r /var/lib/mongodb 2077 sudo apt-get remove --purge mongodb 2078 sudo apt-get install mongodb 2079 sudo service mongodb start 2080 mongo


sudo wget https://downloads.mongodb.com/compass/mongodb-compass_1.14.1_amd64.deb
 1993  sudo dpkg -i mongodb-compass_1.14.1_amd64.deb
 1994  mongodb-compass

install mong db in nodejs | tutorials in nodejs

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 


"mongoose": "^6.7.2",

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({})



AI Tools

 Midjourney oter.ai aiva googlegemin dall-e copilot jasper copilot openaiplayground