本文整理匯總了TypeScript中mongoose.Schema.index方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Schema.index方法的具體用法?TypeScript Schema.index怎麽用?TypeScript Schema.index使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mongoose.Schema
的用法示例。
在下文中一共展示了Schema.index方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: before
before((done: MochaDone) => {
let schema = new mongoose.Schema({
name: { type: String }
});
schema.index({ name: 'text' });
testModel = mongoose.model('testSearchModel', schema);
testServer
.create({ resources: [testModel] })
.then((createdServer: testServer.Server) => {
server = createdServer;
done();
})
.catch(done);
});
示例2:
rate: number;
cc: string;
exchangedate:Date;
}
export interface ICurrency extends mongoose.Document, ICurrencyProps {
}
export const CurrencySchema: mongoose.Schema = new mongoose.Schema({
txt: {
type: String,
required: true
},
rate: {
type: Number,
required: true
},
cc: {
type: String,
required: true
},
exchangedate:Date
}, {timestamps: true});
CurrencySchema.plugin(require("mongoose-paginate"));
CurrencySchema.index({cc: 1});
/**
* METHODS
*/
export const CurrencyModel: IModel<ICurrency> = mongoose.model<ICurrency>("Currency", CurrencySchema);
示例3: function
stuff: { type: String, lowercase: true, trim: true }
}
});
schema.add({
mixedArray: {
type: [mongoose.Schema.Types.Mixed],
required: true
}
}, 'prefix');
schema.eachPath(function (path, type) {
path.toLowerCase();
type.sparse(true);
}).eachPath(cb);
schema.get('path');
schema.index({
name: 1,
binary: -1
}).index({}, {});
schema.indexes().slice();
schema.method('name', cb).method({
m1: cb,
m2: cb
});
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());
});
示例4: require
var mongoose = require('mongoose');
var ThingSchema = new mongoose.Schema({
userid: {
type: String,
required: true
},
expireAt: {
type: Date,
required: true,
default: function() {
// 60 seconds from now
return new Date(new Date().valueOf() + 7260000);
}
}
});
// Expire at the time indicated by the expireAt field
ThingSchema.index({ expireAt: 1 }, { expireAfterSeconds : 0 });
export = mongoose.model('Recovery', ThingSchema);
示例5: createSalt
algorithm: {
type: String,
select: false
},
role: {
type: String,
enum: ['none', 'admin'],
default: 'none'
},
created: Date,
modified: Date,
});
UserSchema.index({
'username': 1,
'name.first': 1,
'name.last': 1
});
function createSalt() {
return crypto.randomBytes(16).toString('hex');
}
function hashPassword(password, salt, algorithm) {
var hash = crypto.createHash(algorithm);
hash.update(salt + password);
return hash.digest('hex');
}
UserSchema.methods.validPassword = function(password) {
return this.password === hashPassword(password, this.salt, this.algorithm);
示例6: Schema
description?: string;
}
export interface StoryInterface extends StoryProps, Document {}
const StorySchema: Schema = new Schema(
{
name: {
type: String,
minlength: 3,
maxlength: 255,
required: true
},
owner: {
type: Schema.Types.ObjectId,
required: true
},
description: {
type: String
}
},
{ timestamps: true }
);
StorySchema.index({ owner: 1, name: 1 });
export const StoryModel: Model<StoryInterface> = model<StoryInterface>(
"Story",
StorySchema
);
示例7: Schema
export interface MessageInterface extends MessageProps, Document {}
const MessageSchema = new Schema(
{
name: {
type: String,
minlength: 3,
maxlength: 255,
uppercase: true,
required: true
},
text: String,
story: {
type: Schema.Types.ObjectId,
required: true,
ref: "Story"
},
compiledSource: String,
messagesLoaded: [Schema.Types.ObjectId]
},
{ timestamps: true }
);
MessageSchema.index({ story: 1, name: 1 }, { unique: true });
export const MessageModel: Model<MessageInterface> = model<MessageInterface>(
"Message",
MessageSchema
);
示例8: next
/**
* name - имя пользователя
* lastname - фамилия пользователя
* regDate - дата регистрации
* login - логин
* pass - пароль
*/
const userSchema = new mongoose.Schema({
lastname: { type: String, required: true, validate: [nameValidator, nameValidationErrorText] },
login: { type: String, required: true, unique: true, validate: [loginValidator, loginValidationErrorText] },
name: { type: String, required: true, validate: [nameValidator, nameValidationErrorText] },
pass: { type: String, required: true, validate: [passValidator, passValidationErrorText] },
regDate: { type: Date, required: true },
});
userSchema.index({ name: 'text', lastname: 'text' });
/** Cохраняем не пароль, а его хеш, используя bcrypt */
userSchema.pre<IUserDocument>('save', async function(next) {
if (!this.isModified('pass')) {
return next();
}
try {
const salt = await bcrypt.genSalt();
this.pass = await bcrypt.hash(this.pass, salt);
return next();
} catch (err) {
return next(err);
}
示例9: it
it('should process a complex example', () => {
const nestedSchema = new Schema(
{
thing: {
type: Schema.Types.ObjectId,
ref: 'Thing',
},
},
{},
);
const nestedItemSchema = new Schema(
{
user: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
},
priority: {
type: Number,
required: true,
default: 0,
},
},
{
_id: false,
id: false,
},
);
const mainSchema: any = new Schema(
{
name: {
type: String,
required: true,
index: true,
placeholder: 'PLACEHOLDER_NAME',
},
referencedDocument: {
type: Schema.Types.ObjectId,
ref: 'RefDoc',
immutable: true,
required: true,
},
stringOptionsWithDefault: {
type: String,
required: true,
default: 'defaultVal',
enum: ['defaultVal', 'Option2'],
},
setting_type: {
type: String,
},
setting_value: {
type: Number,
min: 0,
validate: function validate(val: any) {
if (this.setting_type === 'foo') {
if (val > 1) {
return false;
}
}
return true;
},
},
enabled: {
type: Boolean,
required: true,
default: false,
},
nestedSchema: nestedSchema,
nestedInline: {
prop: {
type: Number,
required: true,
},
},
nestedEmptyInline: {},
nestedItems: {
type: [nestedItemSchema],
},
},
{
strict: true,
},
);
mainSchema.foo = 'bar';
mainSchema.searchable = true;
mainSchema.index({
name: 'text',
});
const input = generateInterface('MainInterface', mainSchema);
const output = `interface IMainInterfaceINestedSchema {
thing?: string;
_id?: string;
}
//.........這裏部分代碼省略.........
開發者ID:JamesHenry,項目名稱:mongoose-schema-to-typescript-interface,代碼行數:101,代碼來源:generate-interface.spec.ts
示例10:
import * as mongoose from 'mongoose';
const NAME = 'Bet';
var schema = new mongoose.Schema({
userId: {type: 'objectId', required: true},
leagueId: {type: 'objectId', required: true},
matchDay: {type: 'number', required: true},
matches: [{
_id: {type: 'objectId', required: true},
result: {
goalsHomeTeam: {type: 'number', required: true},
goalsAwayTeam: {type: 'number', required: true}
}
}]
}, {autoIndex: false});
schema.index({userId: 1, leagueId: 1, matchDay: 1}, {unique: true});
var Bet = mongoose.model(NAME, schema);
export default Bet;