目录
monggose会根据shcemaType将文档值转换成指定的类型
SchemaTypes是在使用Mongoose时,用于定义MongoDB文档模型中字段的数据类型的一种概念。在Mongoose中,每个字段都有一个关联的SchemaType,它定义了该字段的数据类型、验证规则等信息。
String、Number、Date、Buffer、Boolean、ObjectId、Array、Map、Dcimal128、Schema、Mixed、UUID
const schema = new mongoose.Schema({
name: String,
binary: Buffer,
living: Boolean,
updated: { type: Date, default: Date.now },
age: { type: Number, min: 18, max: 65 },
mixed: mongoose.Schema.Types.Mixed,
_someId: mongoose.Schema.Types.ObjectId,
decimal: mongoose.Schema.Types.Decimal128,
array: [],
ofString: [String],
ofNumber: [Number],
ofDates: [Date],
ofBuffer: [Buffer],
ofBoolean: [Boolean],
ofMixed: [mongoose.Schema.Types.Mixed],
ofObjectId: [mongoose.Schema.Types.ObjectId],
ofArrays: [[]],
ofArrayOfNumbers: [[Number]],
nested: {
stuff: { type: String, lowercase: true, trim: true }
},
map: Map,
mapOfString: {
type: Map,
of: String
}
});
// example use
const Thing = mongoose.model('Thing', schema);
const m = new Thing;
m.name = 'Statue of Liberty';
m.age = 50;
m.updated = new Date;
m.binary = Buffer.alloc(0);
m.living = false;
m.mixed = { wang: { 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.map = new Map([['key', 'value']]);
m.save();
const mongoose = require('mongoose');
const schema = new mongoose.Schema({
name: {
type: String,
select: true,
required: true,
validate: v => v.length > 4,
get: v => v +"aaaa",
set: v => "aaaa" + v,
alias: 'i',
immutable: true
}
});
const CatModel = mongoose.model('Cat', schema);
async function stduyFn() {
const cat = new CatModel({name: 'sss'});
try {
await cat.save();
cat.name = '111111'
cat.i = 'dadsadas'
await cat.save();
console.log(cat.name)
} catch (err) {
}
}
stduyFn()
alias会添加一个虚拟属性,映射到path为name上,当设置immutable为true,更改cat.i和cat.name并不会成功更改。
查看SchemaType配置,关系
mongoose.SchemaType是所有SchemaTyps的基类,schema.path('field')是SchemaTyps的实例
console.log(schema.path('name'))
console.log(mongoose.Schema.Types.String.prototype.__proto__ == mongoose.SchemaType.prototype) // true
console.log(schema.path('name') instanceof mongoose.SchemaType) // true
console.log(schema.path('name') instanceof mongoose.Schema.Types.String) // true
转Number
const mongoose = require('mongoose');
const schema = new mongoose.Schema({
age: Number,
});
const Car = mongoose.model('Car', schema);
async function stduyFn() {
const cat = new Car({ age: '15' });
const cat1 = new Car({ age: true })
const cat2 = new Car({ age: false })
const cat3 = new Car({ age: { valueOf: () => 83 } })
try {
await cat.save();
await cat1.save();
await cat2.save();
await cat3.save();
} catch (err) {
}
}
stduyFn()
转String
const mongoose = require('mongoose');
const schema = new mongoose.Schema({
number: Number,
});
const Car = mongoose.model('Car', schema);
async function stduyFn() {
const cat = new Car({ number: '1666' });
const cat1 = new Car({ number: 1555 });
const cat2 = new Car({ number: { valueOf: () => 1032 } })
try {
await cat.save();
await cat1.save();
await cat2.save();
} catch (err) {
}
}
stduyFn()
转Boolean
?true、'true'、1、'1'、'yes'都为true,false、'false'、0、'0'、'no' 都为false