Download the Mongoose module

  • npm install mongoose –save

CMD open mondb

  1. Mongo open mongo
  2. Mongod connected mongo
  3. Nodejs connects to mongodb through Mongoose
  4. Ship Schema object and convert to

Add, delete, change and check API

Model.update(condition,doc,[options],[callback]); Condition: The condition of the update, which requires an object. Parameter doc: The content to be updated. The requirement is an object. Parameter [options] : optional parameter, also an object. Parameter [callback] : An optional parameter that requires a callback function. Model.updateMany() Model.updateOne() foodModel.insertOne() Model.find() Model.remove() Model.updateOne() Model.updateMany()Copy the code

Skip (number) find().limit(page).skip((page-1)*pageSize);

 eg: page  ("That night." )  pageSize("Number of data items per page")
 let pageSize=req.body.pageSize || 2 ;
 let page=req.body.page||1 ;
 db.col.find().limit(page).skip((page-1)*pageSize);

Copy the code

In the mongo
s e t set
gte
o r or
The and method

Mongodb $set update operator {$set: { "details.make": "zzz"}} (>) is greater than $gt (<) is less than $lt (> =) is greater than or equal to - $gte (< =) less than or equal to - $lteeg: db.col.find({likes : {$gt : 100}})
        $or 
        db.getCollection('testOrAnd').  find( {  $or: [{"value": {$gte:300}}] })
        $and 
        db.getCollection('testOrAnd').find({$or: [{$and: [{"state1":11}, {"state2":22}]}, {"value": {$gte:300}}]})
                 

Copy the code

Connect to database and create Schema object add, delete, change and query

//1 Introduce mongoose module
const mongoose = require('mongoose');
//2 Connect to the database
mongoose.connect('mongodb://localhost/admin', {useNewUrlParser: true.useUnifiedTopology: true});

//3 Database connection object
const db = mongoose.connection;
db.on('error'.console.error.bind(console.'connection error:'));
db.once('open'.function() {
  // we're connected!
  console.log('Connection successful')});// //4 Create a Schema object associated with the collection
// const userSchema = new mongoose.Schema({
// us:{type:String,required:true},
// ps:{type:String,required:true},
// age:Number,
// sex:{type:Number,default:0}
// });

// //5 Convert Schema objects into data models
// const User=mongoose.model('User',userSchema); // This data object is associated with the collection (' collection name ', Schema object)

// Insert data
// User.insertMany({us:'wangyi',ps:"123",age:16})
// // returns a Promise object '
// .then((data)=>{
// console.log(data)
// console.log(' Inserted successfully ')
// })
// .catch(()=>{
// console.log(' failed to insert ')
// })


/ / delete
// User.remove({})
// // returns a Promise object '
// .then((data)=>{
// console.log(data)
// console.log(' Deleted successfully ')
// })
// .catch(()=>{
// console.log(' delete failed ')
// })


// Query data
// User.find({})
// // returns a Promise object '
// .then((data)=>{
// console.log(data)
// console.log(' query successful ')
// })
// .catch(()=>{
// console.log(' query failed ')
// })
Copy the code

Nodemailer mailbox verification code POP3/SMTP/IMAP NPM install Nodemailer must be enabled

const nodemailer=require('nodemailer');

// Create the object that sends the mailbox
let transporter=nodemailer.createTransport({
    host:'smtp.163.com'.// Sender email qq via lib/well-know/service.json
    port:465./ / the port number
    secure:true.//true for 456,false for othersports
    auth: {user:'[email protected]'.// Sender email address
        pass:'CXHOUMREVWRLKRAP'/ / SMTP authentication code}})function send(mail,code){
    // Email message
    let  mailobj={
      from: 'It's a nice day' 
      
       '
      @163.com>.// sender address
      to: mail, // list of receivers
      subject: "1902".// Subject line
      text: 'Your verification code is${code}, valid for five minutes.// plain text body
      //html: "Hello world? " // html body
    }
    return new Promise((resolve,reject) = >{
      transporter.sendMail(mailobj,(err,data) = >{
         console.log(err)
         console.log(data)
          if(err){
            reject()
          }else{ resolve() } }); })}module.exports={send}
Copy the code

API document generation

npm install apidoc -g

Create an apidoc.json file

{
  "name": "example"."version": "0.1.0 from"."description": "apiDoc basic example"."title": "Custom apiDoc browser title"."url" : "http://localhost:3000"
}
Copy the code

Run -i to enter directory -o to output directory

apidoc -i myapp/ -o apidoc/ -t mytemplate/