【问题标题】:Different field name for database / schema数据库/架构的不同字段名称
【发布时间】:2016-01-26 15:59:40
【问题描述】:

TL:DR;是否可以在文档中有一个字段foobarbaz 以不同的名称f 存储?

我目前正在计划一个新的数据库架构,其中将包含大量文档(预计一天有 10 到 100k 个文档)。所以我想缩短字段名称,因为它们占用空间宠物文档。

但我也想在我的架构中拥有可读的名称。所以我的问题是:是否可以将架构字段与具有不同名称的文档字段连接起来?

架构:name
文档:n

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    试试mongoose-aliasfield

    插件旨在为您在数据库上的文档编写短键,但允许您在读取获取的文档时使用长的描述性名称。这将减少存储数据所需的存储空间,无需记住短键含义。

    var mongoose = require('mongoose');
    var fieldsAliasPlugin = require('mongoose-aliasfield');
    
    /*Describe a Schema*/
    var PersonSchema = new Schema({
        't' : {'type': Date, 'index': true, 'alias': 'timestamp'},
        'n' : {'type' : String, 'alias': 'name'},
        's' : {'type' : String, 'alias': 'surname'},
        'p' : {
            'a' : {'type' : String, 'alias': 'profile.address'},
            'pn': {'type' : String, 'alias': 'profile.phone_number'}
        }
    });
    
    /*Add field alias plugin*/
    PersonSchema.plugin(fieldsAliasPlugin);
    

    现在您的架构已创建,您可以使用别名字段名称来描述模型的实例

    var person = new Person({
        'timestamp' : new Date(),
        'name'      : 'Jhon',
        'surname'   : 'Doe',
        'profile.address': 'Rue de Morgane',
        'profile.phone_number': '051-123456 78',
    });
    
    person.save();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-30
      • 2018-01-05
      • 2012-07-27
      • 1970-01-01
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多