与mongoose查询
这是我的结构文件夹
– express_example
| —- app.js
| —-模型
| ——– songs.js
| ——– albums.js
| —-和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); module.exports = SongSchema;
album.js
var mongoose = require('mongoose') , Schema = mongoose.Schema , SongSchema = require('./songs') , ObjectId = Schema.ObjectId; var AlbumSchema = new Schema({ name: {type: String, default: 'songname'} , thumbnail: {type:String, default: './public/images/album/unghoangphuc/U1.jpg'} , date: {type: Date, default: Date.now()} , songs: [SongSchema] }); mongoose.model('Album', AlbumSchema);
我怎么能把相册ID代码查询专辑放在album.js
文件中
例:
var mongoose = require('mongoose') , Album = mongoose.model('Album'); app.get('/posts/:id', function(req, res, next) { Album.findById(req.params.id, function(err, album) { // album is available here }); });
请参阅http://mongoosejs.com/docs/finding-documents.html以了解有关查找文档的更多信息。
PS:这是我第三次回答你的问题:)