mongodb导入JSON文件

 mongoimport -d antd -c rules --jsonArray --file rules.json

数组格式的数据一定要加上 --jsonArray ,否则会报错

Schema写法

var schema = new Schema({
  name:    String,    //m.name = 'Statue of Liberty';
  binary:  Buffer,    //m.binary = new Buffer(0);
  living:  Boolean,      //m.living = false;
  updated: { type: Date, default: Date.now },  //m.updated = new Date;
  age:     { type: Number, min: 18, max: 65 },  //m.age = 125;
  mixed:   Schema.Types.Mixed,            //m.mixed = { any: { thing: 'i want' } };         //m.markModified('mixed');//混合值通过该方法改变
  _someId: Schema.Types.ObjectId,  //m._someId = new mongoose.Types.ObjectId;
  array:      [],  //m.array.push(1);
  ofString:   [String],  //m.ofString.push("strings!");
  ofNumber:   [Number],  //m.ofNumber.unshift(1,2,3,4);
  ofDates:    [Date],  //m.ofDates.addToSet(new Date);
  ofBuffer:   [Buffer],  //m.ofBuffer.pop();
  ofBoolean:  [Boolean],  
  ofMixed:    [Schema.Types.Mixed],            //m.ofMixed = [1, [], 'three', { four: 5 }];
  ofObjectId: [Schema.Types.ObjectId],  
  nested: {
    stuff: { type: String, lowercase: true, trim: true }
  }  //m.nested.stuff = 'good';
})
// example use

var Thing = mongoose.model('Thing', schema);

var m = new Thing;
m.name = 'Statue of Liberty';
m.age = 125;
m.updated = new Date;
m.binary = new Buffer(0);
m.living = false;
m.mixed = { any: { thing: 'i want' } };
m.markModified('mixed');
m._someId = new mongoose.Types.ObjectId;
m.array.push(1);
m.ofString.push("strings!");
m.ofNumber.unshift(1,2,3,4);
m.ofDates.addToSet(new Date);
m.ofBuffer.pop();
m.ofMixed = [1, [], 'three', { four: 5 }];
m.nested.stuff = 'good';
m.save(callback);

schema常用字段解析

var schema3 = new Schema({
  test: {
    type: String,
    lowercase: true, // 总是将test的值转化为小写
    uppercase: true, // 总是将test的值转化为大写
    required:true, //设定是否必填
    default:'star', //设定默认值
    index:true, //设定索引值
    unique:true, //索引值唯一
    sparse:true, //是否启用稀疏索引
    match:RegExp, //判断是否通过正则验证
    enum:Array, //判断test值是否包含于enmu对应的数组中
    min:Number, //判断对应值是否大于等于给定值
    max:Number, //判断对应值是否小于等于给定值
    trim:true //去除数据前后的空格
    capped:1024 //限定大小最大为1024字节
    validate:function,为此属性添加一个验证器函数,如demo1所示
    getfunction//为这个属性定义一个定制的getter Object.defineProperty()。如demo2所示
    setfunction//定义此属性的自定义设置Object.defineProperty()。如demo2所示
  }
});
// 初始化schema对象
// demo1: validate
var EmailTemplateSchema = new Schema({
fileName: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your first name']
    }
});
//初始化对象时添加验证,验证传入的值是否合法
var validateLocalStrategyProperty = function (property) {
    return (!this.updated || property.length);
};
// 将传入的值四舍五入后存储
// demo2: get/set
var numberSchema = new Schema({
  integerOnly: {
    type: Number,
    get: v => Math.round(v),
    set: v => Math.round(v),
    require:true
  }
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值