Nodejs operation mongodb two (mongoose operation mongodb)

One, nodejsuse inmongodb

Second, mongoosethe basic steps used

  • 1. Installation package

  • 2. Connect to the database

    const mongoose = require('mongoose');
    // 连接到数据库
    mongoose.connect('mongodb://localhost/nest_cms');
    // 如果有用户名与密码的连接方式
    // mongoose.connect('mongodb://用户名:密码@地址:端口号/数据库');
    // mongoose.connect('mongodb://root:123456@localhost/nest_cms');
    
  • 3. Definitionschema

    // 使用命令查看数据库中的表 show collections 或者定义新的
    const OrderSchema = mongoose.Schema({
          
          
      order_id: String,
      uid: Number,
      trade_no: String,
      all_price: Number,
      all_num: Number,
    })
    
  • 4. Create a data model

    mongoose.modelYou can pass in two parameters or three parameters. However, it is recommended to use three parameters, when the collection name may be inconsistent with the model name

    • The mongoose.model(参数 1:模型名称(首字母大写),参数 2:Schema)third parameter of the two parameters will be Morgan based on the lowercase letter of the first parameter
    • Three parametersmongoose.model(参数 1:模型名称(首字母大写),参数 2:Schema, 参数 3:数据库集合名称)
    const Order = mongoose.model('Order', OrderSchema, 'order');
    
  • 5. Perform a series of operations

    // 实例化数据模型,创建数据的时候需要save
    const order1 = new Order({
          
          
      order_id: '2',
      uid: 5,
      trade_no: '333',
      all_price: 20,
      all_num: 4,
    })
    order1.save()
    
    // 查询数据
    Order.find({
          
          }, (err, docs) => {
          
          
      if (err) {
          
          
        console.log('查询错误');
      }
      console.log(JSON.stringify(docs));
    })
    

3. Basic operations of adding, deleting, modifying and checking

  • 1. Increase data

    The added data must use the instantiated object and then call the save()function

    const order1 = new Order({
          
          
      order_id: '2',
      uid: 5,
      trade_no: '333',
      all_price: 20,
      all_num: 4,
    })
    order1.save()
    
  • 2. Delete data

    // 删除数据
    Order.deleteOne({
          
          _id: '5e0fd41ca53860217c5831bd'}, (err) => {
          
          
        if (err) {
          
          
            console.log('删除失败');
        }
        console.log('删除成功');
    })
    
  • 3. Modify the data

    // 修改数据
    Order.updateOne({
          
          _id: '5e0fd4bb08ad70786fd8ac84', all_num: 2}, {
          
          all_num: 5}, (err, res) => {
          
          
        if (err) {
          
          
            console.log('修改数据错误')
        }
        // { n: 1, nModified: 1, ok: 1 }
        console.log(res);
    });
    
  • 4. Query data

    Order.find({
          
          }, (err, docs) => {
          
          
        if (err) {
          
          
            console.log('查询错误', err)
        }
        console.log(JSON.stringify(docs));
    })
    

Fourth, mongoosemodularization and packaging of individual links

  • 1. Another mongooseconnection method

    // 第一步
    const mongoose = require('mongoose');
    
    // 第二步连接数据库
    mongoose.connect('mongodb://root:123456@localhost/nest_cms', {
          
           useNewUrlParser: true }, (err) => {
          
          
        if (err) {
          
          
            console.log('连接错误');
            return;
        }
        console.log('mongodb连接成功');
    });
    
  • 2. Separately encapsulate the connection into a module (other places are quoted in simple mode, and only connect once)

    // 第一步
    const mongoose = require('mongoose');
    
    // 第二步连接数据库
    mongoose.connect('mongodb://root:123456@localhost/nest_cms', {
          
           useNewUrlParser: true }, (err) => {
          
          
        if (err) {
          
          
            console.log('连接错误');
            return
        }
        console.log('mongodb连接成功');
    });
    
    module.exports = mongoose;
    

Guess you like

Origin blog.csdn.net/kuangshp128/article/details/103855723