当前位置: 首页 > news >正文

哪些行业需要网站有哪些内容脚本外链生成工具

哪些行业需要网站有哪些内容,脚本外链生成工具,西安网站建设有那些公司好,优秀企业网站有哪些目录 官方文档 简介 SchemaType 示例 配置SchemaType规则 通用规则 特定schemaType规则 String Number Date Map monggose会根据shcemaType将文档值转换成指定的类型 官方文档 Mongoose v8.0.3: SchemaTypes 简介 SchemaTypes是在使用Mongoose时,用于…

目录

官方文档

简介

SchemaType

示例

配置SchemaType规则

通用规则

特定schemaType规则

String

Number

Date

Map

monggose会根据shcemaType将文档值转换成指定的类型


官方文档

Mongoose v8.0.3: SchemaTypes

简介

SchemaTypes是在使用Mongoose时,用于定义MongoDB文档模型中字段的数据类型的一种概念。在Mongoose中,每个字段都有一个关联的SchemaType,它定义了该字段的数据类型、验证规则等信息。

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 useconst 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();

配置SchemaType规则

通用规则

  • required:布尔或函数,如果为true,则代表该值必传
  • default: 默认值
  • select:布尔,查询时是否投影
  • validate:函数,属性值验证
  • get: 函数,使用 Object.defineProperty() 定义该属性的自定义 getter
  • set:函数,使用 Object.defineProperty() 定义该属性的自定义 setter
  • alias:字符串,定义一个虚拟属性用于get、set此path
  • immutable:布尔、设置此path的值不可更改
  • index:布尔,是否将此属性设置索引,提高查询文档的速度
  • unique:布尔,是否将此值定义为该集合唯一的属性值
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

特定schemaType规则

String
  • lowercase: 布尔,是否始终对值调用 .toLowerCase()。如果设置为 true,则始终将值转换为小写
  • uppercase: 布尔,是否始终对值调用 .toUpperCase()。如果设置为 true,则始终将值转换为大写
  • trim: 布尔,是否始终对值调用 .trim()。如果设置为 true,则始终将值的前导和尾随空格去除。
  • match: 正则表达式,检查值是否与给定的正则表达式匹配
  • enum: 数组,该数组列出了值的所有可能取值
  • minLength: 数字,检查值的长度是否不小于给定的数字
  • maxLength: 数字,检查值的长度是否不大于给定的数字
Number
  • min: 数字,检查值是否大于或等于给定的最小值
  • max: 数字,检查值是否小于或等于给定的最大值
  • enum: 数组,检查值是否严格等于给定数组中的一个值
Date
  • min: 日期,检查值是否大于或等于给定的最小日期
  • max: 日期,创建一个验证器,检查值是否小于或等于给定的最大日期
Map
  • of:map的key类型默认为String,可以用of指定SchemaType

monggose会根据shcemaType将文档值转换成指定的类型

转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

http://www.hkea.cn/news/639932/

相关文章:

  • wordpress 4.6 中文版沈阳seo
  • 文件管理软件天津搜索引擎优化
  • 九亭网站建设全国疫情高峰时间表最新
  • 青岛网站建设公司武汉seo收费
  • mvc网站建设的实验报告怎么做优化
  • 有官网建手机网站千锋教育培训多少钱费用
  • b2c交易模式的网站有哪些百度营销客户端
  • flash 学习网站重庆网站seo多少钱
  • 年终总结ppt模板免费下载网站小红书seo排名规则
  • 自己架设网站口碑营销的产品有哪些
  • 湖北省网站备案最快几天天津百度推广排名优化
  • app在线开发制作平台seo网络优化前景怎么样
  • 商务网站的基本情况网站建设工作总结
  • 山西建设厅网站网络销售怎么聊客户
  • 软装素材网站有哪些seo网络排名优化哪家好
  • 邯郸市做网站建设网络口碑营销案例分析
  • 罗湖网站建设联系电话西安核心关键词排名
  • 如何编写网站电脑清理软件十大排名
  • 怎么给企业制作网站seo关键词排名优化哪好
  • 高仿服装网站建设西安百度关键词推广
  • 网站单页面怎么做的百度seo站长工具
  • 网站建设谢辞企业营销型网站有哪些
  • 免费网站制作申请行业关键词一览表
  • 网站建设费关键词排名提高方法
  • 搭建淘宝客网站源码最近发生的新闻事件
  • 网站模版网网站关键词排名优化价格
  • 做网站去哪里全国免费发布广告信息平台
  • 靖江做网站湖南seo服务电话
  • 工程建设科学技术奖申报网站友情链接交换标准
  • 做网站后期为什么续费链交换