zoukankan      html  css  js  c++  java
  • mongoose 文档(一) schemas

    1、定义shcema

    在mongoose里一切都由schema开始。每一个schema对应一个mongoDB collection 并且在那个collection里面定义了documents的模型。

    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    
    var blogSchema = new Schema({
      title:  String,
      author: String,
      body:   String,
      comments: [{ body: String, date: Date }],
      date: { type: Date, default: Date.now },
      hidden: Boolean,
      meta: {
        votes: Number,
        favs:  Number
      }
    });

    之后如果你想增加额外的键,使用 Schema#add方法

    在blogSchema中的每一个key定义了在document中的一个属性,将转换为它相关的Schema类型。例如,我们已经定义了的title将转换为String Schema类型并且date将转换为Date Schema 类型。键也可以指定为嵌套对象包含更多的 key/type 定义(例如上面的'meta'属性)。

    合法的Schema 类型是

    • String
    • Number
    • Date
    • Buffer
    • Boolean
    • Mixed
    • ObjectId
    • Array

    查看更多

    Shema不仅定义了document的结构和构造了属性,还定义了document实例方法、静态Model方法复合索引和被称作middleware的document生命周期钩子。

    2、创建一个Model

    使用schema定义,需要将blogSchema转换为能够工作的Model。为此,我们把它传给

    mongoose.model(modelName, schema)

    var Blog = mongoose.model('Blog', blogSchema);
    // ready to go!

    3、实例方法

    Model的实例是document。document有很多内置的实例方法。我们也可以定义自己的document实例方法。

    // define a schema
    var animalSchema = new Schema({ name: String, type: String });
    
    // assign a function to the "methods" object of our animalSchema
    animalSchema.methods.findSimilarTypes = function (cb) {
      return this.model('Animal').find({ type: this.type }, cb);
    }

     现在我们所有的animal实例有findSiilarTypes方法 可用。

    var Animal = mongoose.model('Animal', animalSchema);
    var dog = new Animal({ type: 'dog' });
    
    dog.findSimilarTypes(function (err, dogs) {
      console.log(dogs); // woof
    });

    重新默认的mongoose document方法可以会导致不可预知的结果。

    4、静态

    在model中添加静态方法很简单。继续animalSchema。

    // assign a function to the "statics" object of our animalSchema
    animalSchema.statics.findByName = function (name, cb) {
      return this.find({ name: new RegExp(name, 'i') }, cb);
    }
    
    var Animal = mongoose.model('Animal', animalSchema);
    Animal.findByName('fido', function (err, animals) {
      console.log(animals);
    });

    5、索引

    MongoDB支持二级索引。在mongoose, at the path level或schema层次定义Schema的索引。当创建复合索引时,在shema定义索引是必要的。

    var animalSchema = new Schema({
      name: String,
      type: String,
      tags: { type: [String], index: true } // field level
    });
    
    animalSchema.index({ name: 1, type: -1 }); // schema level

    当程序启动时,Mongoose为每个在schema定义的索引自动地调用ensureIndex 。Mongoose会连续为每个索引调用ensureIndex,当所有ensureIndex调用成功或发生错误在model发出index事件。建议在生产中禁止这种行为因为索引创建能够导致显著的性能影响。通过给schema设置autoIndex选项为false来禁用行为,或者在connection全局设置选项config.autoIndex为false。

    animalSchema.set('autoIndex', false);
    // or
    new Schema({..}, { autoIndex: false });

     Model#ensureIndexes 方法

    6、Virtuals

    virtual  是你能 get 和 set 但不能保存到 MongoDB 的 document 属性。getter用于格式化或符合的field,而setter用于de-composing一个单值到多值存储。

    // define a schema
    var personSchema = new Schema({
      name: {
        first: String,
        last: String
      }
    });
    
    // compile our model
    var Person = mongoose.model('Person', personSchema);
    
    // create a document
    var bad = new Person({
        name: { first: 'Walter', last: 'White' }
    });

    假设我们想要记录bad的全名,我们这样做:

    console.log(bad.name.first + ' ' + bad.name.last); // Walter White

    或者我们可以在 personSchema 定义一个 virtual属性getter, 因此我们不需每次写这些字符串连接

    personSchema.virtual('name.full').get(function () {
      return this.name.first + ' ' + this.name.last;
    });

    现在,当我们使用虚拟属性name.full,getter函数会被执行并且返回值

    console.log('%s is insane', bad.name.full); // Walter White is insane

    主要如果结果记录转换为object或者JSON,默认不包括virtual。

    通过设置 this.name.full 能设置this.name.first和 this.name.last 是令人高兴的。例如,我们想要分别改变 bad 的 name.first 和 name.last 为 'Breaking' 和 'bad',可以这样:

    bad.name.full = 'Breaking Bad';

    Mongoose也通过它的 virtual property setters让你这么做

    personSchema.virtual('name.full').set(function (name) {
      var split = name.split(' ');
      this.name.first = split[0];
      this.name.last = split[1];
    });
    
    ...
    
    mad.name.full = 'Breaking Bad';
    console.log(mad.name.first); // Breaking
    console.log(mad.name.last);  // Bad

     虚拟属性setter在其他验证前应用。因此上面的例子仍然能工作。即使需要 name.first 和name.last fields。

    作为查询的一部分和对于field选择,只有非虚拟属性有效。

    7、option

    new Schema({..}, options);
    
    // or
    
    var schema = new Schema({..});
    schema.set(option, value);

    有效的选项

  • 相关阅读:
    ElasticSearch大数据分布式弹性搜索引擎使用—从0到1
    使用Fiddler搭建手机调试环境(我做得项目是调试微信的公众号)
    Ajax的三种实现及JSON解析
    jquery ajax调用返回json格式数据处理
    jsp页面格式化数字或时间
    java组装json和提取一个json的例子
    span的onclick事件
    Android Studio创建库项目及引用
    xmlns:android="http://schemas.android.com/apk/res/android的作用是
    Android中attrs.xml文件的使用详解
  • 原文地址:https://www.cnblogs.com/surahe/p/5178654.html
Copyright © 2011-2022 走看看