考虑这是我的文件夹结构
express_example
|---- app.js
|---- models
|-------- songs.js
|-------- albums.js
|-------- other.js
|---- and another files of expressjs
我在文件song.js中的代码
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var SongSchema = new Schema({
name: {type: String, default: 'songname'}
, link: {type: String, default: './data/train.mp3'}
, date: {type: Date, default: Date.now()}
, position: {type: Number, default: 0}
, weekOnChart: {type: Number, default: 0}
, listend: {type: Number, default: 0}
});
mongoose.model('Song', SongSchema);
在文件 albums.js 中
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var AlbumSchema = new Schema({
name: {type: String, default: 'songname'}
, thumbnail: {type:String, default: './images/U1.jpg'}
, date: {type: Date, default: Date.now()}
, songs: [SongSchema]
});
mongoose.model('Album', AlbumSchema);
我可以通过以下方式获得任何模型:
require('mongoose').model(name_of_model);
但是如何通过简单的代码而不是 name_of_model 来要求特定文件夹中的所有模型?在上面的示例中,文件夹 ./models/* 中的所有模型