MongoDB Express Nodes

 Create Database Connection



   

We can't import everywhere mongoConnect as it is a kamur process so to avoid it we add it globally



1) Add ver name _db
2) Assign it to client.db()
3) create a fn and export it






Now to add db we need to just add above function
and keep server like this


To insert on element in database





Since we have already got the date we can directly add this to add all elemnet


We will instead return it so we can use than in controller 

Now to fetch the products


Things to note
1) find() provides a cursor which go one by one and if there are miillion of product it will go like that
2) to array converts those product in a list
3) dont use toFind() if u have million of products...instead use pagination

Fethcing a single product


1) Find the product using find() method and use the id to get that specific product.
2) However,Mongodb cursor is unaware that I only want to find a single product so it will keep on goin down
3) To stop this use next() and then

But we have a error because no productId matches but it is false...the reason is that _id is not a string but specail mongodb object so to filter that Id we need to import id and use objectId method to find the specific product


Next add Product Update 189

 m






Comments

Popular posts from this blog

Form Part 2

Event mini project by Thapa#33

De-mystifying 'this' keyword of Javascript