Mongoose entry and basic additions, deletions, and changes

Execute the following in the test_mongoose directory to generate the package.json file 

npm init --yes

And install mongoose in the current directory, execute the command:

npm i mongoose -S

Then use win+R, enter cmd and press Enter, in the dos window, [ Start the database with the command mongo ]-[New testdb database]-[New users collection and insert a record], as shown below:

Next, create a new js file in the test_mongoose folder: mongooseDemo.js, its content is as follows:

Don't care what {useNewUrlParser:true} means, just add it anyway. If not added, there will be a warning message

const mongoose = require('mongoose')

mongoose.connect("mongodb://127.0.0.1:27017/testdb", {useNewUrlParser:true},function (err) {
    if (err) {
        console.log(err)
        return
    }
    console.log("数据库连接成功!")
})

Execute the command in the test_mongoose directory: node ./mongooseDemo.js  to run the mongooseDemo.js file to test whether the database connection is successful

When the above interface appears, it means that the database connection is successful!

Search record:

const mongoose = require('mongoose')

mongoose.connect("mongodb://127.0.0.1:27017/testdb")

var UserSchema = mongoose.Schema({
    name: String,
    age: Number,
    status:Number
})

var User = mongoose.model('User', UserSchema)//注意,当这里是两个参数时,默认连接的是users表,而不是User表。连接的是复数。

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

Note: In var User = mongoose.model('User', UserSchema), when there are two parameters here, the default connection is the users table, not the User table. If you want to specify a table, just add the table name in the position of the third parameter. This line of code is equivalent to: var User = mongoose.model('User', UserSchema,' users ')

The above interface shows that the database connection is successful and the data is successfully queried!

Add record

const mongoose = require('mongoose')

mongoose.connect("mongodb://127.0.0.1:27017/testdb")

var UserSchema = mongoose.Schema({
    name: String,
    age: Number,
    status:Number
})

var User = mongoose.model('User', UserSchema,'users')

var u = new User({
    name: '王五',
    age: 22,
    status:0
})

u.save(function (err, doc) {
    if (err) {
        console.log(err)
        return
    }
    console.log("成功:",doc)
})

Use the command Ctrl+C to exit the command line and run the mongooseDemo.js file again 

The record is added successfully!

If you want to add a record with a default value, you can use the following form when defining the Schema:

update record

The name: "Wang five" record changed name: "wangwu"

const mongoose = require('mongoose')

mongoose.connect("mongodb://127.0.0.1:27017/testdb")

var UserSchema = mongoose.Schema({
    name: String,
    age: Number,
    status:Number
})

var User = mongoose.model('User', UserSchema,'users')

User.updateOne({ name: "王五" }, { name: "wangwu" }, function (err, res) {
    if (err) {
        console.log(err)
        return
    }
    console.log("更新成功:",res)
})

Delete Record

Delete the record name: "zhangsan"

const mongoose = require('mongoose')

mongoose.connect("mongodb://127.0.0.1:27017/testdb")

var UserSchema = mongoose.Schema({
    name: String,
    age: Number,
    status:Number
})

var User = mongoose.model('User', UserSchema,'users')

User.deleteOne({ name: 'zhangsan' }, function (err, res) {
    if (err) {
        console.log(err)
        return
    }
    console.log("删除成功:",res)
})

Guess you like

Origin blog.csdn.net/qq_40323256/article/details/114255478