-
Make connection to the real database on
MongoDB
service.
- Press New Project button for creating one project for many databases
- Push the button Build a Database for creating first DB
- Choose nearest region & name a cluster
-
Go to Database Access tab for creating first user
- Name this user & create pass (f.e. login admin, password admintest)
- In Built-in Role point choose "Read and write to any database"
-
Go to Network Access tab
- Add IP Address
- For developing from anywhere, in example of many developers, choose/press Allow Access From Anywhere and push Confirm button
- Back to database tab and go to Cluster link
- Go to Collections tab & Load a Sample Dataset for learning or push "Add My Own Data" button
- Name database and collection (noun in plural)
-
To connect for database we should take a link,
- back to home page of Atlas (Mongo DB)
- Go to the necessary project
- Push the Connect button
- Choose Connect your application button, where copy link
- Change in link: user, password, database
- Database name must be between "mongodb.net/" and "?retryWrites", like ..mongodb.net/mydatabase?retryWrites..
- write this link to .env & name variable like DB_HOST
- In web-server install mongoose lib for working with mongoDb service npm i mongoose
-
Add mongoDB connection code
See the Pen 14 - MongoDB connection by Andrii (@imitator) on CodePen.
Also in good practice app should be started after we have connection to mongoDB, means like.then(() => {})console.log('DataBase was connected successfully');app.listen(PORT, console.log(`Server started on port ${PORT}`)),After "db is connected" refactor controllers according to work with mongoDb
-
Model & Schema for MongoDB theory, this point is used for
explanation
After successfully connection to database, we may try to add some object to our mongoDb db (only for explanation, b/c this object will be added every time in restarting application)
-
After "const mongoose = require('mongoose');"
declaration make the code
See the Pen 15 - Schema & model in web-server by Andrii (@imitator) on CodePen.
Schema name named in singular name of collection in some of codes we may see const productSchema = new Schema({}), but this is improved for shortest code
Model name in good practice named from big letter in singular noun, also first argument, in our case "product" named also as a name of collection, but in singular
-
After db was successfully connecter write the code
See the Pen 16 - example of object creating in MongoDB by Andrii (@imitator) on CodePen.
OR the same but for better practice if anything wrong.then(() => {console.log('DataBase was connected successfully');const newItem = {name: 'myBrand',};price: '5222',Product.create(newItem, (error, data) => {if (error) {console.log(error);}returnconsole.log(data);} );})OR the same, bt with Better practice of async functions
See the Pen 16 - A example of object creating in MongoDB with async by Andrii (@imitator) on CodePen.
-
After "const mongoose = require('mongoose');"
declaration make the code