24 days of egg learning notes: mongoose default parameters, modular

Keywords: Front-end Mongoose Database MongoDB Attribute

1. mongoose default parameter: when adding data, if no data is passed in, the data configured by default will be used.

Connect to the database, a warning is reported below, useNewUrlParser:true

This attribute will identify the db needed to verify the user in the url. You do not need to specify it before upgrading. You must specify it before upgrading.

When connecting, the third parameter has a callback function, which we can use until the link is unsuccessful.

var mongoose = require("mongoose");
mongoose.connect(
  "mongodb://127.0.0.1:27017/eggcms",
  { useNewUrlParser: true },
  function(err) {
    if (err) {
      console.log(err);
      return;
    }
    console.log("Database connection successful");
  }
);

Configure default parameters:

var mongoose = require("mongoose");
mongoose.connect(
  "mongodb://127.0.0.1:27017/eggcms",
  { useNewUrlParser: true },
  function(err) {
    if (err) {
      console.log(err);
      return;
    }
    console.log("Database connection successful");
  }
);

// Define data table (Collection) mapping, note: field names must be consistent with the database
var CarSchema = mongoose.Schema({
  name: String,
  price: Number,
  status: {
    type: Number,
    default: 1
  }
});

// Define Model operation database
var CarModel = mongoose.model("Car", CarSchema, "car");

var car = new CarModel({
  name: "BMW three series",
  price: 3000000
});
car.save(function(err) {
  if (err) {
    console.log(err);
    return;
  }
  console.log("Successfully added");
});
// CarModel.find({}, function(err, doc) {
//   if (err) {
//     console.log(err);
//     return;
//   }
//   console.log(doc);
// });

When a parameter is not passed, the default value will be taken, such as the status field of car table.

 

2, Modularity

If we write every script to connect to the database, it's not good. Consider extracting this part.

Model > db.js package and export mongoose

var mongoose = require("mongoose");
mongoose.connect(
  "mongodb://127.0.0.1:27017/eggcms",
  { useNewUrlParser: true },
  function(err) {
    if (err) {
      console.log(err);
      return;
    }
    console.log("Database connection successful");
  }
);
module.exports = mongoose;

Model > db.js define schema and model and export model

var mongoose = require("./db.js");
// Define data table (Collection) mapping, note: field names must be consistent with the database
var CarSchema = mongoose.Schema({
  name: String,
  price: Number,
  status: {
    type: Number,
    default: 1
  }
});
// Define Model operation database
var CarModel = mongoose.model("Car", CarSchema, "car");

module.exports = CarModel;

External use (import the corresponding model in app.js, which can be used)

var CarModel = require("./model/car");

CarModel.find({}, function(err, doc) {
  if (err) {
    console.log(err);
    return;
  }
  console.log(doc);
});

Sleep you

Posted by grungefreak on Sun, 03 May 2020 08:26:24 -0700