本文整理匯總了TypeScript中mongoose.Schema.static方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Schema.static方法的具體用法?TypeScript Schema.static怎麽用?TypeScript Schema.static使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mongoose.Schema
的用法示例。
在下文中一共展示了Schema.static方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1:
},
create: {
type: Date,
"default": Date.now
},
description: {
type: String
}
});
schema.static("updateAuthor", (author: {}, description: string) => {
return Author
.update({
"_id": author
}, {
"$set": {
"description": description
}
})
.exec();
});
schema.static("updateByAge", (ageLimit: number, text: string) => {
return Author
.where("age")
.gte(ageLimit)
.update({
"$set": {
description: text
}
示例2: function
});
schema.path('a', mongoose.Schema.Types.Buffer).path('a');
schema.pathType('m1').toLowerCase();
schema.plugin(function (schema, opts) {
schema.get('path');
opts.hasOwnProperty('');
}).plugin(cb, {opts: true});
schema.post('post', function (doc) {}).post('post', function (doc, next) {
next(new Error());
});
schema.queue('m1', [1, 2, 3]).queue('m2', [[]]);
schema.remove('path');
schema.remove(['path1', 'path2', 'path3']);
schema.requiredPaths(true)[0].toLowerCase();
schema.set('key', 999).set('key');
schema.static('static', cb).static({
s1: cb,
s2: cb
});
schema.virtual('virt', {}).applyGetters({}, {});
schema.virtualpath('path').applyGetters({}, {});
/* static properties */
mongoose.Schema.indexTypes[0].toLowerCase();
mongoose.Schema.reserved.hasOwnProperty('');
/* inherited properties */
schema.addListener('e', cb);
/* practical examples */
var animalSchema = new mongoose.Schema({
name: String,
type: String
});
示例3: Schema
import {Schema,Document,Model} from "mongoose";
import {getModels} from "./helpers";
export const RoomModelSchema = new Schema({
model_id: {type: String, required: true},
model_name: {type: String, required: true},
sync_date: {type: Date, required: true}
});
RoomModelSchema.static("getModels",getModels);
export interface IRoomModel extends Document{
model_name: string;
Plan: Array<string>;
count: number;
}
export interface IRoomModelModel extends Model<IRoomModel>{
getModels: ( callback: (error: Error, data: {}) => void ) => void ;
}
示例4:
guid: {type: String},
complex: Schema.Types.Mixed,
entity : {type: String},
model : {type: String},
category : {type: String},
group : {type: String},
level : {type: String},
space : { type: String},
data: Schema.Types.Mixed,
checkouts: Schema.Types.Mixed,
geometry: Schema.Types.Mixed,
material: Schema.Types.Mixed,
});
BimSchema.static("getBims",getBims);
BimSchema.static("getBimsCount",getBimsCount);
BimSchema.static("getBimByGuid",getBimByGuid);
BimSchema.static("getBimComplex",getBimComplex);
export interface IBim extends Document{
guid: string;
complex: Object;
entity : string;
model : string;
category : string;
group : string;
level : string;
space : string;
示例5: Schema
// author_id?: string;
author?: IUser| ObjectID;
artist?: string;
title: string;
description?: string;
videourl: string;
thumbnailurl: string;
}
const videoSchema: Schema = new Schema({
author: {type: Schema.Types.ObjectId, ref: 'User'},
artist: {type: String},
title: {type: String, required: true},
description: {type: String},
videourl: {type: String, required: true, unique: true},
thumbnailurl: {type: String, required: true, unique: true}
});
videoSchema.static('findAll', function (cb: {(err: Error, videos: IVideo[]): void}): Promise<IVideo[]> {
return this.find(cb);
});
videoSchema.static('findByAuthor', function (authorId: string, cb: {(err: Error, videos: IVideo[]): void}): Promise<IVideo[]> {
return this.find({author: authorId}, cb);
});
export const Video: Model<IVideo> = mongoose.model<IVideo>('Video', videoSchema);
}
export = Video;
示例6: Schema
export interface IUser extends Document {
name: string;
email: string;
password: string;
}
const userSchema: Schema = new Schema({
name: {type: String, required: true, unique: true},
email: {type: String, required: true, unique: true},
// we don't want the password to be fetched by default
password: {type: String, required: true, select: false}
});
userSchema.static('findAll', function (cb: {(err: Error, user: IUser[]): void}) {
return this.find(cb);
});
userSchema.static('authenticateUser', function (identifier: string, password: string, cb: {(err: Error, user?: IUser): void}) {
// we need to ask mongoose for the password property this time ...
this.find({$or: [{name: identifier}, {email: identifier}]}, {name: 1, email: 1, password: 1}, (findErr: Error, users) => {
if (findErr) {
cb(findErr);
return;
}
if (users.length > 0) {
// we assume that there is at most one user !
let user: IUser = users[0];
user.compare(password, (bcryptErr, isAuth) => {
if (bcryptErr) {
cb(bcryptErr);
return;
示例7: function
let productSchema = new mongoose.Schema({
name: {type:String, required: true},
brand: {type: String, required: true},
category: {type: String, required: true},
price: {type: Number, required: true}
});
productSchema.methods.getAll = function(cb) {
return Product.find({}, cb);
};
productSchema.methods.getById = function(productId, cb) {
return Product.find({ productId: this._id }, cb);
};
productSchema.static('findByName', function(productName, cb) {
return this.model('Product').find({ productName: this.name }, cb);
});
productSchema.methods.create = function(product: IProduct) {
let newProduct = new Product(product);
newProduct.save(function(err) {
if(err) {
console.log(`error: ${err}`);
}
console.log('saved!')
});
};
export const Product = mongoose.model('Product', productSchema);
示例8: findAllByAuthor
author: {},
description: string;
}
export interface IPostModel extends Model<IPost> {
findAllByAuthor(id: string): Promise<IPost[]>
}
const schema = new Schema({
title: String,
create: {
type: Date,
"default": Date.now
},
author: {
type: Schema.Types.ObjectId,
ref: 'Author'
},
description: String
});
schema.static("findAllByAuthor", (author: string) => {
return Post
.find({ author: author })
.lean()
.exec();
});
export const Post = mongoose.model<IPost>("Post", schema) as IPostModel;