Let’s face it, writing MongoDB validations, transformations, and business logic is cumbersome. So we invented Mongoose

As described in Mongoose Chinese, “Mongoose provides a direct, Scheme-based structure for defining your data model. It has built-in data verification, query construction, business logic hooks, etc., right out of the box. “Mongoose can operate the mongodb database conveniently, easily and efficiently, providing a convenient shortcut for developers to add, delete, modify and check data in the database.

Install the mongoose

Use NPM Install Mongoose to install Mongoose

npm install mongoose
Copy the code

Based on using

/ / into the mongoose
const mongoose = require("mongoose")


// Connect to the database
mongoose.connect("Mongo: / / 127.0.0.1:27017 / myapp")

let UserSchema = mongoose.Schema({
    username: String.age: Number.status: Number
})

let User = mongoose.model("user", UserSchema)

let u = new User({
    username: "wangcai".age: 100.status: 1
})

u.save(function (err) {
    if (err) {
        console.log(err)
    }
    console.log("Data saved successfully...")})Copy the code

Running results:

Note that:

  • The first parameter to model is capitalized
  • Model can have a third parameter,let User = mongoose.model("user", UserSchema, "admin")The third parameter is used to specify the collection name
  • When the program callssave()Before writing data to the database
  • When defining a Schema, you can specify default values, for example:Age :{type:Number, default:10}

The query

// Query example
const mongoose = require("mongoose")
mongoose.connect("Mongo: / / 127.0.0.1:27017 / myapp")
let UserSchema = mongoose.Schema({
    name: String.age: Number
})
let ThisModel = mongoose.model("User", UserSchema)
let Obj = new ThisModel({
    name: "xiaoqiang".age: 10
})
Obj.save(function (err) {
    if (err) {
        console.log(err)
    }
    console.log("Data saved successfully...")})/ / query
ThisModel.find({}, function (err, doc) {
    if (err) {
        console.log(err)
    }
    console.log(doc)
})
Copy the code

Query result:

update

Before the update:

// Update the example
const mongoose = require("mongoose")
mongoose.connect("Mongo: / / 127.0.0.1:27017 / myapp")
let UserSchema = mongoose.Schema({
    name: String.age: Number
})
let ThisModel = mongoose.model("User", UserSchema)
ThisModel.updateOne({ _id: "5d6cd43be93a8e41146ded63" }, { age: "1000" }, function (err, data) {
    if (err) console.log(err)
    console.log(data)
})
Copy the code

Output result:

Database update:

delete

// Delete the sample
const mongoose = require("mongoose")
mongoose.connect("Mongo: / / 127.0.0.1:27017 / myapp")
let UserSchema = mongoose.Schema({
    name: String.age: Number
})
let ThisModel = mongoose.model("User", UserSchema)
ThisModel.deleteOne({ _id: "5d6cd43be93a8e41146ded63" }, function (err, data) {
    if (err) console.log(err)
    console.log(data)
})
Copy the code

Output result:

The end of the

This article is a small summary of the learning process, inevitably there are shortcomings, I hope to be helpful to friends learning.