Sunday, June 26, 2022
HomeWordPress DevelopmentMongoDB connections utilizing mongoose - 3

MongoDB connections utilizing mongoose – 3


Good day Guys as we speak i’m going to point out you find out how to create mongoDB atlas connnection in Specific.
I’ve already lined the folder construction of MERN and MongoDB setup in my earlier 2 blogs so please test it out after which come again right here.

  1. MERN CRUD -Setup – https://dev.to/shubhamtiwari909/mern-crud-setup-148a
  2. https://dev.to/shubhamtiwari909/2-mern-mongodb-setup-434g

Lets get began…

  • So, Now we have created a folder named Backend in our venture and inside it we now have created two file referred to as mongo.js and Queries.js

  • Open the mongo.js in your VScode editor and write this code inside it.


const mongoose = require("mongoose");

const Database =
  "mongodb+srv://Your_UserName:Your_Password@cluster0.d9wx7.mongodb.internet/Your_Database_Name?retryWrites=true&w=majority";

module.exports = async () => {
  await mongoose.join(Database, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  }).then((res) => {
    console.log("linked efficiently")
  }).catch((err) => {
    console.log("error")
  });;

  return mongoose
};

Enter fullscreen mode

Exit fullscreen mode

  • Kindly fill Your username,password and database identify within the Url
  • We’re exporting the database connection utilizing module.export and utilizing async operate for the connection methodology.
  • It’s going to extablish a reference to mongo db atlas and can return a promise , if the connection is successfull, it would resolve the promise and make a connection and if the connection is failed , then it would reject the promise and provides an error
  • If you happen to dont have mongoose put in please test my weblog ”
    MERN CRUD – setup” , i’ve given detailed explaination there.



Creating the Schema –

  • Keep in mind in our Backend folder, we now have created a folder identify “Schema” and contained in the “Schema” folder we now have file named “Schema.js”, open that file and write this code there .
const mongoose = require('mongoose');

const userSchema = mongoose.Schema({
    identify: {
        kind: String,
        required: true
    },
    e-mail: {
        kind: String,
        required: true
    },
    quantity: {
        kind: String,
        required: true
    }
})

module.exports = mongoose.mannequin('customers',userSchema)
Enter fullscreen mode

Exit fullscreen mode

  • Now we have created our schema with 3 fields identify, e-mail and quantity together with some attributes like datatype which these going to retailer and required true property which implies the sphere is a required area and can’t be left empty.
  • Then we now have created our mannequin utilizing “mongoose.mannequin(‘consumer’,userSchema)”, Right here ‘consumer’ is the gathering identify which can maintain all the information , its like a desk in MySQL.
  • Then we now have exported this mannequin utilizing module.export and can import it in our Queries.js file with mongodb connection

  • Now open Queries.js and import the connection from mongo.js file and Schema from Schema.js file

const mongo = require("./mongo");
const userSchema = require("./Schema/Schema");
.
.
.
relaxation of the code
Enter fullscreen mode

Exit fullscreen mode

  • Right here we now have imported the connection methodology of mongo and our Schema from Schema.js file and is able to use these to carry out our CRUD operation in MERN.

Thats it for this put up, i’ll proceed this sequence within the subsequent weblog the place i might be discussing some modules which we’re going to use in our Backend code
THANK YOU FOR READING THIS POST AND IF YOU FIND ANY MISTAKE OR WANTS TO GIVE ANY SUGGESTION , PLEASE MENTION IT IN THE COMMENT SECTION.
^^You possibly can assist me by some donation on the hyperlink under Thanks👇👇 ^^
☕ –> https://www.buymeacoffee.com/waaduheck <–

Additionally test these posts as effectively
https://dev.to/shubhamtiwari909/react-hooks-usecontext-1ml2

https://dev.to/shubhamtiwari909/redux-combinereducers-mk0

https://dev.to/shubhamtiwari909/introduction-to-tailwind-best-css-framework-1gdj

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments