用猫鼬查询
问题描述:
这是我的结构文件夹
-express_example
| ---- app.js
| ----型号
| -------- songs.js
| -------- albums.js
| ----和expressjs的另一个文件
this is my structure folder
-- express_example
|---- app.js
|---- models
|-------- songs.js
|-------- albums.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);
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 以了解有关查找文档的更多信息.
see http://mongoosejs.com/docs/finding-documents.html to learn more about finding docs.
PS:这是我第三次回答您的问题:)
PS: this is the third time that I answered your question :)