zoukankan      html  css  js  c++  java
  • Node_进阶_7

    Node进阶第七天

    一、复习

    一、索引

      数据库中,根据一个字段的值,来寻找一个文档,是很常见的操作。比如根据学号来找一个学生。这个学号是唯一的。只要有学号,就能唯一确认一个学生的文档。学号这个属性,就非常适合建立索引。这样查找就非常简单了。

    explain来查看这个寻找过程。

      这个语句,能够查看检索的过程。

    建立索引,索引建立之后是2的原因是id也是索引。

    这样,今后通过name寻找student文档的时候,速度非常快,因为能够快速的从索引表中,找到这个文档。

    缺点就是插入每条数据的时候,事件变慢了,效率低了。但是换回来的就是寻找的速度快。

    索引这个属性,所有文档都不能相同。

    {unique:true}

    二、Mongoose

     是一个将Javascript对象与数据库产生关系的一个框架,object related model。操作对象,就是操作数据库了;对象产生了,同时也持久化了。

      这个思路是Java三大框架SSH中Hibernate框架的思路。彻底改变了人们使用数据库的方式。

      http://www.mongoose.com

    //引包,并不需要引用mongodb这个包

    const mongoose = require('mongoose');

    mongoose.connect('mongodb://localhost/test');

     

    const Cat = mongoose.model('Cat', { name: String });

     

    const kitty = new Cat({ name: 'Zildjian' });

    kitty.save().then(() => console.log('meow'));

    上面的代码中,没有一个语句是明显的操作数据库,感觉都在创建类、实例化类、调用类的方法,都是在操作对象,但是数据库同步被持久了。

    创建一个模型

    mongoose.model(‘模型名字’,{“name”:String,”age”:Integer});

    就可以被实例化

    var kitty = new Cat({name:’ZildJian’});

    然后这个实例就可以被save。

    来个实例

    db.js:

    //引包

    const mongoose = require('mongoose');

     

    //创建数据库连接

    var db = mongoose.createConnection('mongodb://localhost/hoho');

     

    //监听open事件

    db.once('open', function (callback) {

        console.log('数据库成功连接');

    });

     

    //向外暴露db对象

    module.exports = db;

    student.js:

    var mongoose = require('mongoose');

    var db = require('./db');

     

    //创建了一个schema结构。

    var studentSchema = new mongoose.Schema({

        name: { type: String },

        age: { type: Number },

        sex: { type: String }

    });

     

    //创建了一个模型,就是学生模型,就是学生类。

    //类是基于schema创建的。

    var studentModel = db.model('mongoose', studentSchema);

    //向外暴露

    module.exports = studentModel;

    基于类的方法(之前是基于对象的方法):

    db.js:

    //引包

    const mongoose = require('mongoose');

     

    //创建数据库连接

    var db = mongoose.createConnection('mongodb://localhost/hoho');

     

    //监听open事件

    db.once('open', function (callback) {

        console.log('数据库成功连接');

    });

     

    //向外暴露db对象

    module.exports = db;

    student.js:

    var mongoose = require('mongoose');

    var db = require('./db');

     

    //创建了一个schema结构。

    var studentSchema = new mongoose.Schema({

        name: { type: String },

        age: { type: Number },

        sex: { type: String }

    });

     

    //创建静态方法

    studentSchema.statics.zhaoren = function (name, callback) {

        return this.model('student').find({ name: name }, callback);

    }

     

    //创建修改的静态方法

    studentSchema.statics.xiugai = function (conditions, update, options, callback) {

        this.model('student').update(conditions, update, options, callback);

    }

     

    //创建了一个模型,就是学生模型,就是学生类

    //类是基于schema创建的。

    var studentModel = db.model('student', studentSchema);

     

    //向外暴露

    module.exports = studentModel;

    app.js:

    //定义了一个模型,学生模型,"学生类"

    var Student = require('./models/student');

     

    /*

    //实例化了一个学生类

    var xiaoming = new Student({"name":"小明","age":12,"sex":"男"})

     

    //保存这个学生类 这个save是一个基于对象的操作

    xiaoming.save(function(){

        console.log('存储成功');

    });

    */

     

    //这是一个基于类的操作,表现力更加丰富(工厂)

    // Student.create({

    //     'name': '小红',

    //     'age': 13,

    //     'sex': '女'

    // }, (err) => {

    //     console.log('保存成功');

    // });

     

    Student.zhaoren('小红', function (err, result) {

        console.log(result);

    });

     

    Student.xiugai({ "name": "小红" }, { $set: { "age": 30 } }, {}, function () {

        console.log('改年龄成功');

    });

     

    //我命令的是student类,没有命令db,并不是直接操作数据库的语句。

    mongoose首先要想明白一件事儿,所有的操作都不是对数据库尽心地。所有地操作都是对类或者对象来操作的,数据库的持久化自动完成了。

    创造schema->定义一些schema的静态方法->创造模型

    创造schema用什么语句? new mongoose.schema({});

    创造模型用什么语句?db.model(“Student”,schema 名字);

    前台界面:不操作数据库,只操作类!

    图书管理系统

    借助mongoose做一个图书管理系统。

    db.js:

    //引包

    const mongoose = require('mongoose');

     

    //创建数据库连接

    var db = mongoose.createConnection('mongodb://localhost/hoho');

     

    //监听open事件

    db.once('open', function (callback) {

        console.log('数据库成功连接');

    });

     

    //向外暴露db对象

    module.exports = db;

    student.js:

    var mongoose = require('mongoose');

    var db = require('./db');

     

    //创建了一个schema结构。

    var studentSchema = new mongoose.Schema({

        name: { type: String },

        age: { type: Number },

        sex: { type: String }

    });

     

    //创建静态方法

    studentSchema.statics.zhaoren = function (name, callback) {

        return this.model('student').find({ name: name }, callback);

    }

     

    //创建修改的静态方法

    studentSchema.statics.xiugai = function (conditions, update, options, callback) {

        this.model('student').update(conditions, update, options, callback);

    }

     

    //创建了一个模型,就是学生模型,就是学生类

    //类是基于schema创建的。

    var studentModel = db.model('student', studentSchema);

     

    //向外暴露

    module.exports = studentModel;

    app.js:

    //定义了一个模型,学生模型,"学生类"

    var Student = require('./models/student');

     

    /*

    //实例化了一个学生类

    var xiaoming = new Student({"name":"小明","age":12,"sex":"男"})

     

    //保存这个学生类 这个save是一个基于对象的操作

    xiaoming.save(function(){

        console.log('存储成功');

    });

    */

     

    //这是一个基于类的操作,表现力更加丰富(工厂)

    // Student.create({

    //     'name': '小红',

    //     'age': 13,

    //     'sex': '女'

    // }, (err) => {

    //     console.log('保存成功');

    // });

     

    Student.zhaoren('小红', function (err, result) {

        console.log(result);

    });

     

    Student.xiugai({ "name": "小红" }, { $set: { "age": 30 } }, {}, function () {

        console.log('改年龄成功');

    });

     

    //我命令的是student类,没有命令db,并不是直接操作数据库的语句。



  • 相关阅读:
    回调函数(callback)是什么?
    类和对象的关系
    前端性能优化十四个规则:
    响应时间过长而导致网页问题的原因?
    给老爸更换电脑
    Notes for "Python in a Nutshell"
    Debian Jessie升级至Stretch小记
    将LibreOffice文档转换为豆瓣日记
    将Emacs Org任务树导出至Freeplane思维导图
    GNU/Linux下Freeplane的界面渲染问题
  • 原文地址:https://www.cnblogs.com/eret9616/p/9111475.html
Copyright © 2011-2022 走看看