Learning Summary of Basic Use of Mongoose Module

Learning Summary of Basic Use of Mongoose Module

Mongoose is a database management tool module of MongoDB in Node environment, which can transform data in the database into Js objects.

Installation and Connection of Database

Install using npm package management tools.
npm i mongoose
The introduction of mongoose is consistent with the introduction of normal module const mongoose = require ('mongoose');

How does Mongoose connect to the database and determine the type of data in the database (i.e. skeleton building)

mongoose.connect('mongoose://127.0.0.1:27017/databaseName',function(err){ 
	// Mongoose://-> mongodb protocol 127.0.0.1-> local IP 27017-> port databaseName-> database name
	if (!err){
		console.log('Database is connected');
	} else {
		console.log(err);
	}
})
// The skeleton construction of database
const mongoose = require ('mongoose'); // Import module

const userSchema = new mongoose.Schema({ // Determine the content and data type of the database collection
    username: String,
    password: String
});

const userModle = mongoose.model('users', userSchema); //Create a database collection with the same skeleton

Addition, deletion and modification of database

Add data

const userModle = mongoose.model('users', userSchema); //Use the database collection created above

// first
var instance = new userModel (); 
instance.username='shifu';
instance.password=123456;
instance.save(function(err){  //save adds data to the current data set
    if (err) {
       console.log('Save failed');  
       return;
    }
 });

// second
var instance = new userModle({
	username:'shifu',
	password:123455
});
nstance.save(function(err){
    if (err) {
       console.log('Save failed');  
       return;
    }
 });

Delete data

var id="57e24521a755e1154039a403"; // mongodb's primary keys are distributed randomly. When deleting and modifying databases, they must pass through the primary keys.
userModel.findById(id,function(err,doc){ // Get the data doc for the current primary key
   if(!doc){
      return next(new NotFound("Doc not found"))
   }else{
      doc.remove(function(){ //Delete the current collection data in the database
         console.log('Delete successful');
      })
   }
});

Change data

var id="57e24521a755e1154039a403"; // mongodb's primary keys are distributed randomly. When deleting and modifying databases, they must pass through the primary keys.
userModel.findById(id,function(err,doc){ // Get the data doc for the current primary key
   if(!doc){
      return next(new NotFound("Doc not found"))
   }else{
      doc.username = 'xxx'; // Change the value of the current data in the database
   }
});

Find data

userModel.find({},function(err,docs){ // The first parameter is the search range (full selection when not written), and the second parameter is the eligible data set.
	console.log(docs);
});

Tags: Mongoose Database MongoDB npm

Posted on Fri, 04 Oct 2019 00:52:25 -0700 by mashamit