MongoDB three basic: mongoose for data CRUD

A, mongoose introduction

mongooseMongodb convenient operation is carried out in a node.js asynchronous environment object modeling tools. mongooseNodeJS is driven, not driven as other languages.

Mongoose has two characteristics
  • 1, by thinking relational database to non-relational database design
  • 2, based on the mongodbmodule drive, to simplify the operation

Two, mongoose installation and use

2.1 Installation
npm install mongoose --save
2.2 mongoose introduced and connect to the database
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');

If there is mongodb database account password:

/**通过mongodb://username:password@host1:port/database_name?authSource指定验证前面身份信息的数据库来源 */
mongoose.connect('mongodb://账号:密码@ip地址:27017/集合名称?authSource=admin',{ 
    useNewUrlParser: true,
});
2.3 Definitions Schema

Schema is a data model will be used in the mongoose, can be understood as defined in the table structure; each of the schema maps to a collection mongodb is, it does not have the ability to operate the database, only the fields of the database a set of type specification defines.

var UserSchema=mongoose.Schema({
    name: String,
    age:Number,
    status:'number'
})
2.4 Creating a data model

Good definition Schema, is the generation take over Model. model generated by the model is a schema, the database may be operated.
mongoose.model which can be passed two parameters can also be passed three parameters:

  • mongoose.model (Parameter 1: model name (first letter capitalized), argument 2: Schema)
  • mongoose.model (Parameter 1: model name (first letter capitalized), argument 2: Schema, Parameter 3: collection name)

If passed, then two parameters: Userthe model will default to operate with Usera set of the plural form of the model of the same name, such as creating a model by the following method, then this model will operate usersthis set:

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

If then passed three parameters: default model name set operating parameters defined by the third, as the following code, the model Userspecifies operation userthis collection:

var User=mongoose.model('User', UserSchema, 'user');
2.5 Finding Data
/**模型.find({查询条件}, (err, data)=>{回调函数}) */
User.find({}, (err, docs)=>{
    if(err){
        console.log(err);
        return;
    }
    console.log(docs);
});
2.6 increase data
/*模型实例化*/
const news = new News({
    title:'新闻标题',
    author:'joyitsai',
    pic:'01.png',
    content:'新闻内容',
    status: 1
});
/*实例.save((err,docs)=>{回调函数}) 来将数据保存到数据库*/
news.save((err, docs)=>{
    if(err){
        console.log('添加数据出错');
        return;
    }
    console.log(docs);
});
2.7 modify the data
News.updateOne(
    {'_id':'5cf5e613ba3c6298a8734973'}, //条件
    {title: '这是一则新闻111'},     //要更新的内容
    /*回调函数*/
    (err, docs)=>{
        if(err){return console.log('更新数据失败');}
        console.log(docs);
    }
)
2.8 to delete data
News.deleteOne(
    {'_id':'5cf5e613ba3c6298a8734973'}, //查找条件
    /*回调函数*/
    (err,docs)=>{
        if(err){return console.log('删除数据失败')}
        console.log(docs);
    }

)
Find successfully saved 2.9
News.updateOne(
    {'_id':'5cf5e613ba3c6298a8734973'}, //条件
    {title: '这是一则新闻111'},     //要更新的内容
    (err, docs)=>{
        if(err){
            return console.log('更新数据失败');
        }
        /**更新数据成功,紧接着查询数据 */
        News.find({},(err, docs)=>{
            if(err){
                return console.log(err)
            }
            console.log(docs);
        })
    }
)

Guess you like

Origin blog.csdn.net/weixin_33724046/article/details/90912682