timestamps: object or boolean - defaults tofalse. If true, Mongoose addscreatedAtandupdatedAtproperties to your schema and manages those properties for you. pluginTags: array of strings - defaults toundefined. If set and plugin called withtagsoption, will only apply that plugin to schemas with...
«Array» an array of strings representing populated paths Gets a list of paths to be populated by this query Example: bookSchema.pre('findOne', function() { let keys = this.getPopulatedPaths(); // ['author'] }); ... Book.findOne({}).populate('author'); Example: // Deep ...
SchemaTypes handle definition of path defaults, validation, getters, setters, field selection defaults for queries and other general characteristics for Strings and Numbers. Check out their respective API documentation for more detail. 数据类型用于定义默认路径, 验证方式, 获取/设置方法,用于数据库查询的...
_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); ...
newSchemaType.prototype.jsonSchema = function() { // Simple types (strings, numbers, bools): const jsonSchema = mongoose.SchemaType.prototype.jsonSchema.call(this); // Date: const jsonSchema = Types.Date.prototype.jsonSchema.call(this); // ObjectId const jsonSchema = Types.ObjectId.prototype....
favoriteFoods : array of strings (*) 1. 2. 3. 4. 5. 采用Mongoose 基础 schema 类型。 如果还想添加更多的键,就请使用 required 或 unique 等简单的验证器(validators),并设置默认值。 详情请参考 Mongoose 文档。 请从personSchema 创建一个名为 Person ...
cat.ofString.push("strings!"); //Array 其中Mixed是mongoose自定义的一种混合类型,因为Mixed没有定义具体内容,可以用{}来使用,以下2种定义形式等价。 javascriptvar animalSchema = new Schema({any: {}}); var animalSchema = new Schema({any: {Schema.Types.Mixed}}); ...
import {z} from 'zod'; import {mongooseZodCustomType} from 'mongoose-zod'; const zodSchema = z.object({ refs: mongooseZodCustomType('ObjectId').array(), data: mongooseZodCustomType('Buffer').optional(), }).mongoose();Don't we still have type safety for options like alias and time...
var Person = new Schema({ title : { type: String, lowercase: true } }); In the example above we specified thelowercaseoption for strings which will lowercase the string whenever it is set. Options are functions that are called on each SchemaType. EachSchemaTypehas its own set of custom...
varschema=newSchema({name:String,binary:Buffer,living:Boolean,updated:{type:Date,default:Date.now},age:{type:Number,min:18,max:65,required:true},mixed:Schema.Types.Mixed,_someId:Schema.Types.ObjectId,array:[],ofString:[String],// You can also have an array of each of the other types ...