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所示
get:function,//为这个属性定义一个定制的getter Object.defineProperty()。如demo2所示
set:function,//定义此属性的自定义设置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
}
});