我编写了我的Schema,但是当我运行Node.js服务器时,显示以下错误:
MySchema.createIndex不是函数
我用它来设置记录的expireAt。这是我的代码:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var config = require('../../config.js');
var tc = new Date();
var te = tc.setSeconds(tc.getSeconds + config.EXPIRE_TOKEN_TIME.ROOM_TOKEN);
var MySchema = new Schema({
name: String,
guide: String,
leader: String,
partecipants_counter : { type: Number, default: 0},
event_counter : { type: Number, default: 0},
createAt: { type: Date, default: tc},
expireAt: { type: Date, default: te},
partecipants: [],
events : [ {
id : Number,
data: String,
user: String
} ]
});
MySchema.createIndex( { "expireAt": 1 }, { expireAfterSeconds: 180 } );
module.exports = mongoose.model(config.DATA_TYPE.ROOM, MySchema);
答案 0 :(得分:0)
语法应为
MySchema.index( { "expireAt": 1 }, { expireAfterSeconds: 180 } );
更一般的例子: -
var animalSchema = new Schema({
name: String,
type: String,
tags: { type: [String], index: true } // field level
});
animalSchema.index({ name: 1, type: -1 }); // schema level