How to Connection of Mongoose in Node Js.

rukonpro profile

Rukon Uddin

08/25/2024

How to Connection of Mongoose in Node Js.
  1. First, make sure you have MongoDB and Node.js installed.
  2. Next install Mongoose from the command line using npm:


$ npm install mongoose - save

Now let’s say we like obscure kittens and want to record every kitten we see on MongoDB. The first thing we need to do is to include Mongoose in our project and open a link to the test database in the locally running example of MongoDB.


    // getting-started.js
    
    const mongoose = require('mongoose');
    main().catch(err => console.log(err));
    
    async function main() {
        await mongoose.connect('mongodb://localhost:27017/test');}
    }
                        


For brevity, let’s assume that all of the following code is in the main () function. With mangoes, everything originates from a schema. Let’s get a reference and define our kitten.


    const kittySchema = new mongoose.Schema({
                            name: String
                           });


So far so good. We’ve got a schema with a property, name, which would be a string. The next step is compiling our schema into a model..


 const Kitten = mongoose.model('Kitten', kittySchema);


A model is a class with which we create documents. In this case, each document will be a kitten with features and behaviors declared as our schema. Let’s make a kitten document that the little guy we just met on the sidewalk outside:


    const silence = new Kitten({ name: 'Silence' });
                    console.log(silence.name); 
                    
                    // 'Silence' Kittens can do magic too, so let's take a at how to add "Speak" 
                    functionality to our document:
                    
                    kittySchema.methods.speak = function speak() {
                    
                    const greeting = this.name ? "Meow name is " + this.name : "I don't have a name";
                    
                   console.log(greeting);
                };


 const Kitten = mongoose.model('Kitten', kittySchema);


Functions added to a schema method property are compiled into model prototypes and expressed in examples of each document:


const fluffy = new Kitten({ name: 'fluffy' });
                   fluffy.speak()


We’re talking kittens! But we still can’t save anything on MongoDB. Each document can be saved in the database by calling the save method. The first argument of callback would be an error if anything happened.


await fluffy.save();
fluffy.speak();


Say time goes by and we want to show all the kittens we see. We can access all kitten documents through our Kitten model.


const kittens = await Kitten.find();
                    console.log(kittens);


We’ve just logged the kittens into our DB console. If we want to filter the names of our kittens, Mongoose MongoDBs support rich query syntax.


 await Kitten.find({ name: /^fluff/ });


It does a search for all documents, including the property of a name beginning with “Fluff” and returns the result as an array of kittens in the callback.